Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.

Similar presentations


Presentation on theme: "1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures."— Presentation transcript:

1 1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

2 2 What shall we learn today? Introduction –What is debugging –Why do we need debugging –What do you need Trace your code –Debugging tools

3 3 Why? The complier will never catch all the bugs –The complier only check syntax errors –It will never check if your code would fulfill your tasks. You are on you own to –make your program correct –make your program robust –make sure what is really going on in your code

4 4 What is debugging? Always make sure you know what you are expecting from the code If your program did not output what you expected, tract it down to the exact point You will find the bug! Go ahead and fix it. your code inputoutput Debugging

5 5 !!! What is the BEST way to mastering C/C++? Viewing examples? Reading C/C++ syntax? Taking exams? NO. The BEST way is through playing with a debugger!

6 6 Debug Tools Debuggers –have full control of the execution of the program set break points check values of variables and structures modify variables dump memories … – depend on platforms UNIX debuggers: e.g., adb, dbx, gdb WIN-PC debugger: e.g., VC++ debugger Integrated development environment (IDE) –Put together editor, complier, linker, debuggers, etc. –e.g., MS Visual Studio

7 7 Observe, Guess and Trace You get bugs, but there is no need to panic! It is very normal. How to catch and fix them? –Create testing cases (normal and abnormal) –Observe the run-time error information, intermediate results, stranger behaviors, and printouts –Guess the possible reasons (which needs experience) –Trace down to the troublesome code lines which needs strategy, tool, experience and patient. –Fix the bug, which will be a piece of cake! It is full of fun doing bug-catching.

8 8 Setting breakpoints Where do you want to stop and check? How to set breakpoints –at a source-code line –at the beginning of a function –when an expression is true –when a variable changes value –when an expression changes value

9 9 Stepping Through Your Code What if you do not want to set so many breakpoints? If you want a careful trace Stepping –run to cursor –step over –step into –step out

10 10 Check and Modify Variables How can access those variable? VC++ debugger provides many windows –Output –Variables –Watch –Quick watch –Call stack –Register –Memory –disassembly

11 11 Simulating Conditions You can test your functions by simulating some “artificial” conditions Modify the value of a variable in debugger, instead of the source code!

12 12 assert ( b != NULL && size > 0 ); assert ( low >= 0 && high <= size-1 ); assert ( low <= high ); #include // Binary search int binarySearch( const int b[], int searchKey, int low, int high, int size ) { int middle; while ( low < high ) { //? middle = ( low + high ) / 2; if ( searchKey == b[ middle ] ) // match return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array else low = middle + 1; // search high end of array } return -1; // searchKey not found } Let’s Rock’n Roll!

13 13 #include int binarySearch( const int b[], int searchKey, int low, int high, int size ) void main() { int a[10] = {1, 3, 5, 6, 7, 10, 19, 25, 38, 88}; int q = 5; // test case 1 // int q = 6; // test case 2 int res; res = binarySearch(a, q, 0, 9, 10); if (res == -1){ cout << “not found!\n”; } else{ cout << q << “is in the database at a[“ << res<< “]\n”; }


Download ppt "1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures."

Similar presentations


Ads by Google