04_Debugging - Discussion 4 Debugging EECS 280 Monday...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 4: Debugging EECS 280 Monday, October 3rd, 2011 1 Introduction A bug is a portion of code which results in the program not functioning correctly. Bugs can be caused by: Syntax Errors, which are caught by the compiler Runtime Errors, which cause the program to loop indefinitely, terminate pre- maturely, or return the dreaded “segmentation fault” error Logic Errors, which result in an incorrect output or behavior Syntax errors are caught by the compiler, and in general are easy to identify and fix. Runtime errors can be identified by commenting out suspicious portions of code, until the program runs successfully. Logic errors can be found by adding cout statements to print key variables at various steps in the program. However, the best way to identify runtime/logic errors is to use a debug tool (gdb). 2 GDB Demo 2.1 Original Code 1 #include <iostream > 2 #include <iomanip > 3 #include "recursive.h" 4 5 using namespace std; 6 7 int factorial(int n) { 8 if(n = 0) 9 return 1; 10 else 11 return n * factorial(n - 1); 12 } 13 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
14 void array_copy(int* from , int* to , int size) { 15 while(size) { 16 to = from; 17 to++; 18 from ++; 19 } 20 } 21 22 int main() { 23 cout << "factorial (3) = " << factorial (3) << endl; 24 int a[] = {1, 2, 3, 4}; 25 int b[4]; 26 int sum = 0; 27 array_copy(a, b, 4); 28 for(int i = 0; i < 4; i++) { 29 sum += b[i]; 30 } 31 cout << "The average of 1, 2, 3, 4 is " << fixed << setprecision (1) 32 << static_cast <double >(sum /4) << endl; 33 list_t list; 34 for(int i = 0; i < 5; i++) { 35 list = list_make(i, list); 36 } 37 list_print(list); 38 cout << endl; 39 cout << "0.3 = " << fixed << setprecision (18) << 0.3 << endl; 40 return 0; 41 } 2.2 Bug 1 First we need the compiler to add debugging information to our executable. This is done by the - g flag. As in: g++ -g recursive.cpp fix_0.cpp -o buggy Running ./buggy results in a segmentation fault. So let’s start debugging, first run gdb buggy . You’ll get a gdb prompt. Use run to run the program: (gdb) run Starting program: . ... 2
Background image of page 2
Program received signal SIGSEGV, Segmentation fault. 0x0000000000401558 in factorial (n=0) at fix_0.cpp:11 11 return n * factorial(n - 1); The backtrace command prints the current stack of function calls. This produces a very long print out. We also see that factorial() is called with n = 0 , which should cause it to terminate. Below is a portion of the backtrace: (gdb) backtrace #0 0x0000000000401558 in factorial (n=0) at fix_0.cpp:11 #1 0x000000000040155d in factorial (n=0) at fix_0.cpp:11 #2 0x000000000040155d in factorial (n=0) at fix_0.cpp:11
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

04_Debugging - Discussion 4 Debugging EECS 280 Monday...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online