Selections in C++: Debugging

Debugging is the process of finding and fixing errors in a program.

As discussed in Section 1.9.1, syntax errors are easy to find and easy to correct because the compiler indicates where the errors came from and why they are wrong. Runtime errors are not difficult to find either, since the operating system displays them on the console when the pro­gram aborts. Finding logic errors, on the other hand, can be very challenging.

Logic errors are called bugs. The process of finding and correcting errors is called debug­ging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. However, for a large, complex program, the most effective approach for debugging is to use a debugger utility.

The C++ IDE tools, such as Visual C++, include integrated debuggers. The debugger utili­ties let you follow the execution of a program. They vary from one system to another, but they all support most of the following helpful features:

  • Executing a single statement at a time: The debugger allows you to execute one statement at a time so that you can see the effect of each statement.
  • Tracing into or stepping over a function: If a function is being executed, you can ask the debugger to enter the function and execute one statement at a time in the function, or you can ask it to step over the entire function. You should step over the entire function if you know that the function works. For example, always step over system-supplied functions, such as pow(a, b).
  • Setting breakpoints: You can also set a breakpoint at a specific statement.

Your program pauses when it reaches a breakpoint and displays the line with the breakpoint. You can set as many breakpoints as you want. Breakpoints are par­ticularly useful when you know where your programming error starts. You can set a breakpoint at that line and have the program execute until it reaches the breakpoint.

  • Displaying variables: The debugger lets you select several variables and display their values. As you trace through a program, the content of a variable is continu­ously updated.
  • Displaying call stacks: The debugger lets you trace all of the function calls and lists all pending functions. This feature is helpful when you need to see a large pic­ture of the program-execution flow.
  • Modifying variables: Some debuggers enable you to modify the value of a vari­able when debugging. This is convenient when you want to test a program with dif­ferent samples but do not want to leave the debugger.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *