Reputation: 11901
I am interested to know on what things I need to concentrate on debugging c code without a debugger. What are the things to look for?
Generally I look for the following:
Any structured approach is very much appreciated.
Upvotes: 1
Views: 5869
Reputation: 215259
A big one you left out is integer overflow. This includes both undefined behavior from overflow of signed expressions, and well-defined but possibly-dangerous behavior of unsigned overflow being reduced mod TYPE_MAX+1
. In particular, things like foo=malloc(count*sizeof *foo);
can be very dangerous if count
came from a potentially untrusted source (like a data file), especially if sizeof *foo
is large.
Some others:
char
when doing anything more than copying values or comparison for equality (otherwise you probably want unsigned char
or perhaps in rare cases, signed char
)./POWER_OF_2
and %POWER_OF_2
(hint: (-3)%8==-3
but (-3)&7==5
).Upvotes: 1
Reputation: 3563
I recommend trying one of the many static code analyzers. Those that I used personally and can recommend:
If you want more details, you can read an article I wrote on that subject.
Upvotes: 1
Reputation: 118510
Most of these errors will be picked up by passing the appropriate warning flags to the compiler.
However from the original list, points 1, 5, 6, 7, 8 are very much worth checking as a human, some compiler/flag combinations however will pick up on unhandled values, pointers to automatic memory, and off-by-one errors in array indexing etc.
You may want to take a look at such things as mudflap, valgrind, efence and others to catch runtime cases you're unaware of. You might also try splint, to augment your static analysis.
For the unautomated side of things, try statically following the flow of your program for particular cases, especially corner cases, and verify to yourself that it appears to do the right thing. Try writing unit tests/test scripts. Be sure to use some automated checking as discussed above.
If your emphasis is on testing without any test execution, splint might very well be the best place to start. The technique you want to research is called static code analysis.
Upvotes: 4