SLIS logo
SLIS - Courses - LIBR252AB - G. Liu - Debugging



Tips on Debugging C/C++/Java Programs


Now you all got a taste of how debugging a computer program is like. You probably will agree with me that getting rid of syntax error is easy. And you also probably agree with me that it is the logical errors that make a programmer's life really miserable but fun at the same time. It seems to be a right time to share some debugging tricks with you.

Before making any changes to your program, the first thing you need to do is to see through the codes and understand what is going on behind, to visualize the logical flow of computing.

Based on the understanding of logical flow of computing, combined with "symptoms" you see, you would have some vague idea about what could have possibly gone wrong. Then to better locate the trouble spots and to isolate the problem, you often need to "expose" hidden process of computing by intercepting internal information flow.

This is where the trick of inserting System.out.println() -- or printf() in C/C++ -- lines comes into play. This trick of intercepting internal information flow and revealing hidden process of computing is often referred to as "tracing".

At the top level, you can trace the beginning, exiting, and return value of a function. For instance, with a function WowWow(), we can do something like this,


	int WowWow() {
	 int var_a; 	/* .... declaration of local variables */

	 System.out.println("DEBUG:: entering WowWow() ...");
	 // printf("DEBUG:: entering WowWow() ...\n");

	 /* main body of the function */

	 System.out.println("DEBUG:: leaving WowWow() ...");
	 // printf("DEBUG:: leaving WowWow() ...\n");

	 return 1;
	}

This trick is often useful to find out where something causes the system to crash.

Once having located the troubled function, next step is to identify the specific block of codes in that function that is causing trouble. This could be done by employing similar tactics to trace entering and leaving a logic block of codes, and even of a single line of codes.

However, often the problem could be more complicated than that. The spot you have found could be just a "crack" on surface, which might be caused by some unexpected errors somewhere else. It is so especially when that line of codes is a complicated sentence that involve some variables, and the correctness of the sentence depends on the values of those variables that were set or changed earlier somewhere else in the program.

In this case, we often need to resort to tracing value changes of a variable. As you may guess, this can be done in a similar way, by inserting printing lines to track the value setting of the variable at some or all critical points. By following the computing steps and checking on the value changes of variable, you can often get a pretty good idea about where and what has gone wrong.

Following are some typical logical errors that even an experienced programmer may still encounter once in a while:

And so on and on. This is just a quick list of some typical logical errors in programming. But in the real world of programming, you may run into all kinds of surprises. You may be caught up with some bugs, one day even after years of professional programming, that you never suspected in any slightest way. The possibilities are infinite, because the factors and possible interactions and combinations of them are uncountable. Yet, this is exactly what makes programming and debugging so exciting.

Good luck!




Select LIBR252A | LIBR252B to return to the main course page.

This page is maintained by Geoffrey Z. Liu, gliu@wahoo.sjsu.edu. It was last revised on February 1st, 2005.