CMPT 225 Lab 5: Debugging Practice

CMPT 225 Lab 5: Debugging Practice


In this lab we will practice our debugging skills. While some editors may give you some quite useful tools for debugging, you are still well-equipped even if all you have is a vim. In fact the mere print statement goes a long way in debugging.

Compile time errors

Sometimes our program doesn't compile. For example, if you try using a class without comparison operators as a base type in a sortedArrayList, your program won't compile. The good thing is the compiler usually tells us what line of code caused the error and it also gives us a description of the error. If the error description doesn't make too much sense for us, searching it on the web is usually very helpful.

Lab Activity

Try compiling print_patients.cpp. You will get an error. Try figuring out what the error means (if needed search on the web) and fix the code. You should upload the fixed file as part of the deliverables of this lab. When fixed, the program should run as follows:
g++ -o print_patients.o print_patients.cpp 
./print_patients.o 
(Golnar,10)(p1,2)

Run time errors

If the program crashes while running or seems to run but isn't doing what we expect it to do, we can try putting print statements in between lines of code to localize the error (find out which line is causing it).

For example the following program is supposed to take up to n positive numbers from the user where n is an input parameter to the program. If the user enters -1 however, it means he/she is done with entering numbers.
int main(int argc, char** argv){
  int n = atoi(argv[1]);
  int input = -1;
  int ctr = 0;
  while ((ctr < n) &&  (input!=-1)){
     cin >> input;
     if (input <= 0)
        continue;
  }
}
But when we try running it as follows it crashes due to segmentation fault!
./test.o 
Segmentation fault: 11
While we have seen this error description before, it's not useful in telling us what in our code is causing the problem. But we can try putting print statements in between lines of code to determine where the error came from:
int main(int argc, char** argv){
  cout << "before reading n \n";
  int n = atoi(argv[1]);
  cout << "before initializing input \n";
  int input = -1;
  cout << "before initializing ctr \n";
  int ctr = 0;
  cout << "before entering the loop \n";
  while ((ctr < n) &&  (input!=-1)){
     cout << "inside the loop!\n";
     cin >> input;
     if (input <= 0)
        continue;
  }
}
Now, when we run the file we get:
./test.o
before reading n 
Segmentation fault: 11
This tells us the source of our error is between the two print statements "before reading n" and "before initializing input" because the first was printed and the second wasn't. A glance at the line in between tells us we have forgotten to provide the input to the program. So, we try running it correctly this time:
./infinite_while.o 3
before reading n 
before initializing input 
before initializing ctr 
before entering the loop

There is no segmentation fault anymore but the program doesn't seem to do anything either. In fact it seems like it doesn't enter the loop.

At this point, we either immediately realize that the initial value of input (-1) was chosen badly or we need printing its value right before the loop to see that (sometimes it's not as obvious as it is here).

After we change the initial input value to -100, we run the program and this time it looks like it is not ending unless the user enters -1. So, we put a new print statement that prints the value of ctr for us:

int main(int argc, char** argv){
  cout << "before reading n \n";
  int n = atoi(argv[1]);
  int input = -100;
  int ctr = 0;
  while ((ctr < n) &&  (input!=-1)){
     cout << "inside the loop and ctr = " << ctr << endl;
     cin >> input;
     if (input <= 0)
        continue;
  }
}
Running this code reveals that ctr is always 0 which tells us that we forgot increamenting it at the end of while loop. We can fix that error and see that the program starts working as expected.

Lab Activity

Now try debugging the following files, fix the errors, and upload the fixed files as part of deliverables of this lab:
  1. reverse_string.cpp
  2. This is a program that takes a string as input and reverses it. When fixed, it should run as follows:
    g++ -o reverse_string.o reverse_string.cpp 
    ./reverse_string.o Golnar
    ranloG
        
  3. add_doubles.cpp
  4. This is a program that starts with a vector of {1,2} and tries adding the doubles of all elements to the vector and printing it twice. When fixed, it should run as follows:
    g++ -o add_doubles.o add_doubles.cpp 
    ./add_doubles.o 
    1 2 2 4 
    1 2 2 4 2 4 4 8 
    
  5. get_all_permutations.cpp
  6. This is a program that obtains and prints all permutations of a set {1..n} for n starting from 1 and going until 5. When fixed, it should run as follows:
    g++ -o get_all_permutations.o get_all_permutations.cpp 
    ./get_all_permutations.o 
    permutationss of {1..1}:
    1 
    permutationss of {1..2}:
    2 1 
    1 2 
    permutationss of {1..3}:
    3 2 1 
    2 3 1 
    2 1 3 
    3 1 2 
    1 3 2 
    1 2 3 
    permutationss of {1..4}:
    4 3 2 1 
    3 4 2 1 
    3 2 4 1 
    3 2 1 4 
    4 2 3 1 
    2 4 3 1 
    2 3 4 1 
    2 3 1 4 
    4 2 1 3 
    2 4 1 3 
    2 1 4 3 
    2 1 3 4 
    4 3 1 2 
    3 4 1 2 
    3 1 4 2 
    3 1 2 4 
    4 1 3 2 
    1 4 3 2 
    1 3 4 2 
    1 3 2 4 
    4 1 2 3 
    1 4 2 3 
    1 2 4 3 
    1 2 3 4 
    

What to submit?

The fixed version of the following files:
  1. print_patients.cpp
  2. reverse_string.cpp
  3. add_doubles.cpp
  4. get_all_permutations.cpp

Where to submit?

In CourSys under CMPT 225 Lab5 activity.