Lab 1 Debugging

Brad Vander Zanden

The ability to debug a program is one of the most crucial skills that you will need to acquire in order to be an effective programmer. There are three principle ways you can debug a program:

  1. Read the code and try to trace it by hand. This technique often works well once you have managed to localize a bug to a small section of code. It can also work well for the small programs you will write to start this course but typically it becomes impractical for large programs since the bug can appear anywhere in thousands of lines of code.
  2. Insert print statements into your code so that you can:
    1. trace the bug to a small section of code after which you might be able to debug the code by reading it.
    2. determine the values of important variables in your program.
    This is the most common technique for finding bugs and the one we will practice in this lab.
  3. Use a debugging tool, such as gdb or valgrind, to help you locate the bug. You will be introduced to gdb in a later lab.
Generally when you debug a program you must first generate a hypothesis as to why the program is failing. Then you can insert print statements in the parts of the code where you suspect the program is failing in an effort to localize the bug.

However, sometimes you do not even have a clue as to where the bug is occurring. In this case you must start inserting print statements at important points in the code in an effort to localize the bug. Important points in the program include immediately after any housekeeping you perform to start the program and immediately after any important computation performed by the program. When a print statement reveals that the value of a variable is incorrect, you know that the bug in your program must lie in code that precedes the computation of this variable and you can start moving your print statements earlier in the program. You keep moving the print statements earlier until the value of your important variable(s) become correct. At this point you know that the bug must lie between the current location of your print statements and the location of the print statements that first revealed the incorrect value of the variable. Sometimes this section of code is small enough that you can start debugging it by reading the code. Other times you must continue to try to further narrow down the location of the bug by moving your current print statements later in the code, until a value of a variable becomes incorrect, or moving the initial print statements earlier in the code, until they become correct. In any event what you are trying to do is "catch" the bug between the print statements that print correct variable values and print statements that print incorrect variable values.

Let's try this in practice with a simple program. Take a look at sum.cpp which sums a series of numbers and outputs their sum: #include <iostream> using namespace std; int main() { int sum; int value; while (!cin.eof()) { cin >> value; sum += value; } cout << "sum = " << sum << endl; }

This program has two bugs in it and is small enough that you might be able to debug it by simply reading the code. However, let's suppose that you can't figure out what is wrong by simply reading the code.

First compile this code and run it against numbers.txt:

UNIX> ./sum < numbers.txt
We are re-directing input from stdin so that it comes from the file numbers.txt. Look at numbers.txt. It would appear that the sum of the numbers if 100. However, the program is either printing 150 or is printing some extremely big number for sum.

Debugging sum.cpp

There is a good chance that you have no idea what is happening and cannot generate a hypothesis as to what is happening. Thus you need to insert print statements into the program. Remember that good practice is to put print statements after any housekeeping has been done, and after the computation of important variables. There are two places where you should be putting print statements in this program. Think about where you should put the print statements and then click here when you think you know the answer.