COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2.

Slides:



Advertisements
Similar presentations
Utilizing the GDB debugger to analyze programs Background and application.
Advertisements

Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
CS201 - Repetition loops.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
CSE 303 Lecture 13a Debugging C programs
Guidelines for working with Microsoft Visual Studio.Net.
C Slides and captured lecture (video and sound) are available at:
Compiling and Linking. Compiling is quite the same as creating an executable file! Instead, creating an executable is a multistage process divided into.
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
Copyright © 2009 Techtronics'09 by GCECT 1 Presents, De Code C De Code C is a C Programming competition, which challenges the participants to solve problems.
Instructor Notes GPU debugging is still immature, but being improved daily. You should definitely check to see the latest options available before giving.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
Computer Programming I Hour 2 - Writing Your First C Program.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Data Display Debugger (DDD)
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Multiple File Compilation and linking By Bhumik Sapara.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Introduction To Software Development Environment.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
CSCI 4061 Recitation 2 1.
DEBUG.
Chapter 5: Preparing C Programs
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
What Is? function predefined, programmer-defined
Computer Science 210 Computer Organization
CS1101X Programming Methodology
C Programming Hardik H. Maheta.
Compilation and Debugging
Compilation and Debugging
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Debugging with gdb gdb is the GNU debugger on our CS machines.
Computer Science 210 Computer Organization
gdb gdb is the GNU debugger on our CS machines.
Debuggers.
Separate Compilation.
Chapter 11 Introduction to Programming in C
Govt. Polytechnic,Dhangar
C/C++ Basics.
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
Appendix F C Programming Environment on UNIX Systems
What Is? function predefined, programmer-defined
Chapter 15 Debugging.
Debugging.
SPL – PS1 Introduction to C++.
Chapter 15 Debugging.
Presentation transcript:

COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2

Outline TA contact g++ makefile debug

About me

TA contact Tao Li PhD student at CISE Office Hour This Week: Thursday 9 th period at E309

Separate code header file #ifndef _my_stack #define _my_stack int add(int x, int y); // function prototype for add.h #endif.cpp file int add(int x, int y) { return x + y; } Header guards Because header files can include other header files, it is possible to end up in the situation where a header file gets included multiple times.

Compilation: g++ 1.Compiling, in which C++ code is translated into assembly; 2.Assembling, in which assembly is translated into machine language; and 3.Linking, in which calls to functions outside the main file are linked to their definitions. //////////////////////////////////////////////// g++ -c MyStack.cpp g++ -c main.cpp g++ -o stack main.o MyStack.o or //////////////////////////////////////////////// g++ -o stack main.cpp MyStack.cpp -o program_name // compiling and linking to generate program_name, default "a.out" -c // compiling but no linking -g // for debugging, but runs slow

make and makefile make is a system designed to create programs from large source code trees and to maximize the efficiency of doing so. To that effect, make uses a file in each directory called a Makefile. This file contains instructions for make on how to build your program and when. target: dependencies instructions example Note: Build several independent targets in order, below is sample makefile ========================================================== all: target1 target2 target3 target1: dependencies instructions target2:...

Stack A stack is a last in, first out (LIFO) data structure

Main.cpp & Input file if(x == 1) { fscanf(fp1, “ %d”, &y); myStack.Push(y); } else { myStack.Top(y); printf(“%d\n”, y); myStack.Pop(); }

Run./program_name For example:./stack

GNU debugger -- gdb A symbolic debugger is a program that aids programmers in finding logical errors, by allowing them to execute their program in a controlled manner. 1.Enable symbol table 2.Debug the program g++ -g -o stack stack.cpp gdb stack

Use gdb Starting Execution of Program (gdb) run (or r) Quitting gdb (gdb) quit (or q or Ctrl-D) Resuming Execution at a Breakpoint Once you have suspended execution at a particular statement, you can resume execution in several ways: continue (or c) Resumes execution and continues until the next breakpoint or until execution is completed. next (or n) next will execute a function in the current statement in its entirety.

Setting Breakpoints & Print Setting a breakpoint permits you to mark a particular line in your program (called a breakpoint) so that when execution reaches that line, program execution will be suspended, allowing you to enter a gdb command. break function : Set a breakpoint at entry to function function. break filename:linenum :Set a breakpoint at line linenum in source file filename. print expression (or p expression)Displays the value of the expression (usually a variable) once → at the current point of execution.

Example: tst.cpp 1. #include “stdio.h” 2. int summation(int n) { 3. int sum = 0, i; 4. for(i = 1; i<n; i++) { 5. sum += i; 6. } 7. return sum; 8. } 9. int main() { 10. printf(“Summation is %d\n”, summation(100)); 11. return 0; 12. }

Example g++ tst.cpp -o tst./tst Output: Summation is 4950 Actually: 1+2+…+100 = (1+100) * 100 / 2 = 5050 Where is the bug?

Tip: Reduce the input size Change printf(“Summation is %d\n”, summation(100)); To: printf(“Summation is %d\n”, summation(5)); Expected result: = 15 g++ tst.cpp -o tst./tst Output: Summation is 10

gdb Compile: g++ -g tst.cpp -o tst Run gdb: gdb tst List code: l Breakpoint: break 5 Run to bkpnt: r Next step : n Print value: p (variable) Finish: finish Quit: q

The power of PRINTF Add: printf(“i=%d, sum=%d\n”, i, sum); Output: i = 1, sum = 1 i = 2, sum = 3 i = 3, sum = 6 i = 4, sum = 10 Summation is 10