While I don't often agree with Mr. Twink, but I think that (assuming
the OP is using Linux, of which I can see absolutely no hint in his
post) using valgrind early and often is a good thing. Even on
programs that seem to work. I added it to our large regression suite
a while back at work and found a number of defects that way that had
not been otherwise noticed. They were of the class of things (e.g.
off by one errors causing invalid memory reads/writes) that can cause
difficult to diagnose at the customer site memory corruptions, but
often aren't detected in unit testing... I'm thus a big fan of using
valgrind (and in the past purify) at any stage of development...
I also agree with your point to some degree, that being able to find
these things by eye is a good thing. A competent code inspector
should be able to catch return of automatic memory (or putting it into
a struct that is returned).
I hope I didn't give the impression that I would be somehow
opposed to using valgrind and similar programs. I use it my-
self for exactly the purpose you describe, i.e. finding de-
fects and, if I am lucky, getting some hints what kind of
problem I have to look for. But what it's not IMHO is an all-
purpose debugging tool in the sense "write some code, run it
through valgrind and if it doesn't complain everything is
fine, otherwise it tells me where to look". It isn't that
already because to find the mistake one made from what val-
grind outputs can be a rater lengthy process, often not much
simpler than getting from e.g. a segmentation fault to the
exact place where the logical error in the program is. And
it isn't such an all-purpose debugging tool also since it
can only collect data from the parts of a program that have
actually been executed in the test run - running through each
and every possible path in a program can be nearly impossible.
I find it rather problematic to recommend valgrind to a relative
beginner in C programming. To be able to get something out of
the defects valgrind flags one needs to have at least some aware-
ness of the errors one can make. And I doubt very much that
someone who can't debug a <=50 lines program by eye has any
realistic chance of making head or tail of the output of val-
grind. The OP's program output clearly showed that there was a
severe problem and the only information valgrind in this case
adds is that there is an invalid read, but at a place that's
not the one where the principal problem is. Since I have a bit
of experience with C programming and (much too much
with
debugging all I would conclude from it is that I made a mistake
somewhere in the program, using already deallocated memory, ha-
ving passed back a pointer to a non-static, local variable etc.
And I know that the mistake can be at a completely different
place from the one where valgrind found the defect and thus would
not concentrate my attention unnecessarily to just this place. On
the other hand, someone relatively new to programming in C will
rather likely have problems understanding what "invalid read"
actually signifies and assume that the problem is somewhere near
to where valgrind found the defect, spending lots of time looking
for bugs that don't exist. That's at least the conclusion I draw
from my own learning of how to debug and observing what other
people struggle with.
Regards, Jens