Re: Debugging standard C library routines
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 01 Oct 2006 15:27:20 -0400
Richard Heathfield wrote:
Eric Sosman said:
jacob navia wrote:
Frederick Gotham wrote:
Richard Heathfield posted:
Initialise every pointer.
Initializing every pointer does no harm, [...]
It does no harm to the running program, certainly. But
it *does* harm the process of developing the program, by
removing the compiler's ability to warn about certain kinds
of errors.
I beg to differ. I mean, yes, it has the effect that you say it has - on some compilers, anyway - but it doesn't harm the development process at all.
Example:
char *next_field(char **start)
{
char *p /* = NULL */, *q /* = NULL */;
/* Skip white space to find the start of the field: */
p = *start + strspn(*start, " \t\f\r\n");
/* Skip non-whites to find the end of the field: */
p = p + strcspn(p, " \t\f\r\n");
/* Record where the next search should start: */
*start = q + (*q != '\0');
/* Zero-terminate the field just located: */
*q = '\0';
/* Return a pointer to its beginning: */
return p;
}
Great example. Well done. If you omit the initialisation, okay, let's say the compiler issues a warning (despite the fact that it needn't and some don't). But you know and I know that some people will say "oh, it's only a warning, it's fine", and they'll be scratching their heads trying to debug it. Whereas, if you set q to NULL, then it doesn't take a genius to discover that *start is being set to an obviously silly value (probably NULL, possibly 1). So the debugging process is very swift after all.
This programmer you describe seems a bit of an odd fish. He's
reckless enough to ignore warning messages, yet diligent enough to
initialize all his pointers. Sounds like a person with a multiple
personality disorder ...
The cheapest errors are those not made in the first place.
The next-cheapest are those caught by the compiler and fixed
before committing the code.
That's fine, provided people treat diagnostic messages seriously. We have ample evidence here on comp.lang.c that this is not the case.
It's interesting to read this in light of remarks about "drool-
proof languages" on another current thread ...
I feel -- without quantitative evidence, I admit -- that there's
more to be gained from improving a programmer's skills than by trying
to compensate for his deficiencies. A person can be taught to pay
attention to warnings, or to initialize everything in sight so the
warnings go away and their Heisenbugs become reproducible. The first
course seems to me to offer more benefits, on balance, than the second.
Yes, a Heisenbug can be a royal PITA -- but it's not a certainty that
a reproducible error arising from a wrongly-initialized variable will
be reliably detected, either. Even if that variable is a pointer and
the initialization is to NULL, it's not a sure bet that there will be
a catastrophic failure, easily spotted: Maybe the pointer will be used
as the argument to fflush() or the first argument to strtok() or in
some other context where NULL is perfectly legal -- but perhaps not
what was intended.
Wanton initialization of pointers (of any variables,
actually) discourages the compiler's assistance and therefore ought
not to be indulged in.
Giving the program deterministic behaviour by ensuring that all variables are initialised helps the programmer to understand the program better and debug it more quickly, and therefore ought to be encouraged. :-)
Get rid of the STOP sign at the busy intersection, but make sure
that anyone who fails to stop *will* be run over by a cement truck.
Well, there won't be many repeat offenders ... (An old Dilbert strip
involved Dogbert trying to teach common sense to those who lacked it
rather conspicuously. "Larry the auto mechanic liked to smoke cigars
while working on gasoline engines," says Dogbert. "Can anybody think
of a problem this might cause?" The answer comes from someone swathed
in bandages and smoking a cigar: "He gets struck by lightning every
time?")
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Debugging standard C library routines
- From: Richard Heathfield
- Re: Debugging standard C library routines
- References:
- Debugging standard C library routines
- From: achintmehta
- Re: Debugging standard C library routines
- From: Richard Heathfield
- Re: Debugging standard C library routines
- From: Frederick Gotham
- Re: Debugging standard C library routines
- From: jacob navia
- Re: Debugging standard C library routines
- From: Eric Sosman
- Re: Debugging standard C library routines
- From: Richard Heathfield
- Debugging standard C library routines
- Prev by Date: Re: C99 Versus ANSI.
- Next by Date: Re: Integer to "string" conversions
- Previous by thread: Re: Debugging standard C library routines
- Next by thread: Re: Debugging standard C library routines
- Index(es):