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.
g++ -o print_patients.o print_patients.cpp ./print_patients.o (Golnar,10)(p1,2)
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: 11While 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: 11This 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.
g++ -o reverse_string.o reverse_string.cpp ./reverse_string.o Golnar ranloG
g++ -o add_doubles.o add_doubles.cpp ./add_doubles.o 1 2 2 4 1 2 2 4 2 4 4 8
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