Debugging in C Guidelines
To find bugs in your code, follow the steps below.
Frequently Seen C Mistakes
- If you are getting a segmentation fault, check your code
using Valgrind or
gdb. This will help you identify
the line where the problem is taking place.
- Make sure you are generating the expected results by using
diff. In some cases a single character
could make you fail a test.
If your code generates the expected results in grace, but not
in the submit server try Valgrind.
Valgrind can help you identify the use of uninitialized variables
and problems with dynamic memory allocation.
You can also try to set to 0 uninitialized variables and pointer
variables to NULL. This could help you track the problem.
Run splint on your code. For example, splint my_prog.c.
Compile your code with the
gcc option -fsanitize=address (e.g., gcc -fsanitize=address p2.c).
When you run the code you could get some diagnostic information.
If you are running valgrind on your code, probably
you should not compile your code with this option.
If none of the above steps identify the problem,
try the following:
- Simplify the input/case that is generating the problem.
- Once you have simplified the input/case, use the debugger
(gdb) to step through your code. Information on
gdb basics can be found at gdb.
A list of frequently seen mistakes can be found at
Development Strategy for C Programs