Debugging and Syntax Errors in C 1 Debugging
Debugging and Syntax Errors in C++ 1
Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program. 2
Types of Errors l Compilation Errors l l Runtime or Logical Errors l l Program cannot be compiled successfully Program is compiled successfully but runs incorrectly or crashes Which kind of errors is more difficult to detect? 3
l Compilation Errors l Typical Causes of Compilation Errors l Strategy for locating and fixing compilation errors l Runtime/Logical Errors l Typical Causes of Runtime or Logical Errors l Strategy for locating and fixing logical errors l Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs l l 4
Typical Causes of Compilation Errors l Misspelled names l l Mismatched parentheses or double quotes l l i. e. : { } [ ] ( ) " Omitting punctuation symbols l l e. g. : Mixed up lowercase/uppercase letters e. g: ; : , Not closing a multi-line comment /*. . . */ 5
1 2 3 4 5 6 7 8 9 10 11 /********************** / A program that yields compilation errors. /***********************/ #include <iostream> using namespace std; void main( ) { if (strlen(args) > 0) { cout << "Hello, " ; cout << args[0] << "!"; } } else cout << "Hello, you!" ; } Where's the bug? ----------Configuration: test - Win 32 Debug----------Compiling. . . test. cpp H: bioexptesttest. cpp(7) : error C 2143: syntax error : missing '; ' before 'else' H: bioexptesttesttest. cpp(9) : error C 2143: syntax error : missing '; ' before '}' H: bioexptesttest. cpp(9) : error C 2143: syntax error : missing '; ' before '}' Error executing cl. exe. test. exe - 4 error(s), 0 warning(s) 6
There are so many compilation errors! Which error should I fix first? l Fact: Many of the errors listed by the compilers are caused by the errors that appear before them. l What should you do? l Locate and fix the first error listed by the compiler and recompile your program (and repeat the process as long as you need to). 7
I can't find the error at line xxx! l Fact: The bug that corresponds to the compilation error message could appear anywhere before or at the line indicated by the compiler. l What should you do? l Inspect the code backward starting at the line indicated by the compiler (usually just within few lines) l Divide and conquer: Use /* … */ to comment out the block of codes that may contain the bug, then gradually reduce the search range. 8
I don't know what these compilation errors mean! • • • syntax error before `*' template argument 1 is invalid parse error before `)' ANSI C++ forbids declaration `function' with no type l Don't panic! Ignore the error message and just inspect the code around the line indicated by the compiler l Don't give up easily! Try harder for a little longer (After all, it is your code. ) l "Google" the error message for possible solutions l Last resort: Ask TAs or teachers 9
l Compilation Errors l Typical Causes of Compilation Errors l Strategy for locating and fixing compilation errors l Runtime/Logical Errors l Typical Causes of Runtime or Logical Errors l Strategy for locating and fixing logical errors l Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs l l 10
Typical Causes of Runtime Errors l Uninitialized Variables l Forget to create an object using the "new" operator before using the object e. g. : char * c; int len = strlen(c); l Miscalculated index range e. g. : char *s = "abc"; char last. Letter = s[3]; l Implemented a "wrong" solution 11
// A program to compute interest #include <iostream> using namespace std; void main( ) { double interest. Rate, principal, interest; int months; // Input. . . // Calculation. . . // Output. . . } Suppose this program always prints the interest as $0. 00. What could go wrong? How should you approach to locate the bug? 12
Basic approaches to find bugs l Inspect the value of variables l The behavior or the output of your program depend on the values of some variables. 13
Basic Approaches To Locate Bugs l Inspect the value of variables l Select some input values which you know what the output is l Decide which variables to inspect and where to inspect them l Print the value of the variables (to see if they match your expected values) 14
Decide which variables to inspect #include <iostream> using namespace std; void main( ) { double interest. Rate, principal, interest; int months; // Input. . . // Calculation. . . // Output. . . } Input: Does not depend on any variable Calculation: Depends on interest. Rate, principal, and months Output: Depends on interest 15
Output the value of the dependent variables #include <iostream> using namespace std; void main( ) { double interest. Rate, principal, interest; int months; // Input. . . cout << "Before calculation: "; cout << "interest. Rate = " << interest. Rate; cout << "principal = " << principal; cout << "months = " << months; // Calculation. . . cout << "Before output: "; cout << "interest = " << interest; // Output. . . } 16
Debugging part of program l Sometimes you may only want to debug part of your program because … l The rest of the code have already been fully tested l Execution time is long l l e. g: You only want to test the output part but the calculation part takes several hours to complete That's the only part that you write/modify. 17
Debugging only the "Calculation" part … #include <iostream> using namespace std; void main( ) { double interest. Rate, principal, interest; int months; // Input. . . interest. Rate = 0. 12; // 12% principal = 10000; months = 2; // Expected interest is 201 // Calculation …. cout <<"interest = " << interest; // Output. . . } Manually set the value of the dependent variables Check the calculated value against the expected value 18
Debugging only the "Calculation" part … #include <iostream> using namespace std; void main( ) { double interest. Rate, principal, interest; int months; /* // Input. . . */ interest. Rate = 0. 12; // 12% principal = 10000; months = 2; // Expected interest is 201 Optionally, comment the codes that are not subject to testing // Calculation … cout <<"interest = " << interest; /*// Output. . . */ } 19
l Compilation Errors l Typical Causes of Compilation Errors l Strategy for locating and fixing compilation errors l Runtime/Logical Errors l Typical Causes of Runtime or Logical Errors l Strategy for locating and fixing logical errors l Debugging programs with IDE l Interpreting error messages Adding codes to help detecting bugs l 21
How can IDE help you in debugging your programs? l Visual debugger (easy to use) l Allows you to specify where to pause an executing program l Allows you to inspect the value of the variables when a program is paused l Allows you to trace your program execution 22
Running Program in Debugging Mode l Run Main Project (ctrl+F 5) l l Execute the program in normal mode Debug Main Project (F 5) l l Execute the program in debugging mode Debugger can only pause a program when you are running the program in debugging mode 23
Specifying Where To Pause A Program A breakpoint typically represents a location (usually a line of code) where an executing program is paused. To toggle (set or remove) a breakpoint at a line of code, click at the spot in the left grey area next to the line in the editing window. A small red square indicates that a break point is set at that line. Multiple breaks points are allowed. 24
Inspecting Value of Variables When a program is paused, you can view the value of the variables through: "Local Variables" window Automatically shows all the initialized local variables "Watches" window Shows only the variables added manually by the programmer. 25
Breakpoints l Shortcut to see the breakpoints l Opens the breakpoint window and integrates it along with other windows at the bottom of Visual Studio. NET environment. This window contains information concerning all breakpoints that are currently set. 26
In Between Breakpoints When you want to continue running your program, you can use l Continue (F 5) l l continue execution until next breakpoint is encountered Step l l l As simple as Step Into or Step Over Take one "step" of execution and pause How big is one step? 27
Stepping l Step Over (F 10) l l l Execute one line of code and pause Step Into (F 11) l If a line of code involves a method call, go into the method if possible and pause l Otherwise, execute one line of code and pause Step Out (Shift-F 11) l Execute the remaining lines of codes in the current method and pause 28
Other Features l Hexadecimal Display l l Run to cursor (Ctrl-F 10) l l converts all decimal variable values that are in the watch window, local window, and other windows into hexadecimal values. Use the cursor in the editing window as a temporary breakpoint so execution will pause at the cursor. Finish Debugger Session (Shift-F 5) l Stop debugging the program 29
Debug Menu l The Visual Studio. NET Debug menu contains all of the above selections for those that prefer using menus over toolbars. 30
l Compilation Errors l Typical Causes of Compilation Errors l Strategy for locating and fixing compilation errors l Runtime/Logical Errors l Typical Causes of Runtime or Logical Errors l Strategy for locating and fixing logical errors l Debugging programs with IDE Interpreting error messages Adding codes to help detecting bugs l l 31
Interpreting error messages From time to time, you may come across error messages, and error types are: l out-of-memory error 32 l String index out of range l ….
Adding codes to help detecting bugs l You can minimize runtime errors in your program by adding code to check variables against invalid values. l e. g. : // Before calculate interest … if (month < 0 || principal < 0. 0 || interest. Rate < 0. 0) { // Print an error message and maybe // write code to recover from the error. } 33
- Slides: 32