If you are careful and follow some guidelines, the amount of debugging you need to do is minimized. The guidelines are:
1. if (segmentation fault) {
a. Use valgrind to locate the error.
b. Use gdb and the where command.
} else {
if (no results) {
a. Program may have an infinite loop (check your loops).
b. Program is waiting for input.
c. Output statements failed.
} else {
/* incorrect results or different in the submit server */
a. Use the diff command to identify the differences.
b. If using diff does not help, simplify the input as much
as possible to see if you can identify the problem.
c. Step through the code using the debugger.
d. If all the above fails, use printf statements to see
values, and the execution path leading to the problem.
}
}
2. Compile your code with gcc flags -fmudflapth -lmudflap as
they can provide additional information.
gcc flags -fmudflapth -lmudflap my_code.c
You may want to have an alias set that includes these flags.
Do not run valgrind with code compiled with these flags.
3. Try splint on your code.
The valgrind utility can be use to detect dynamic memory problems
and to detect invalid memory accesses. To use valgrind make sure
you compile your code with the -g option.
To run valgrind:
valgrind a.out
If you are using input redirection:
valgrind a.out < public01.in
Other options:
valgrind --leak-check=full a.out /* for details of leaked memory */
valgrind --track-origins=yes a.out /* to track use of uninitialized values */
Additional information about valgrind can be found at http://pages.cs.wisc.edu/~bart/537/valgrind.html and http://valgrind.org/