Re: Debugging standard C library routines
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Sun, 01 Oct 2006 17:49:04 +0000
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.
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.
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. :-)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- Follow-Ups:
- Re: Debugging standard C library routines
- From: Old Wolf
- Re: Debugging standard C library routines
- From: Christopher Layne
- Re: Debugging standard C library routines
- From: Ian Collins
- Re: Debugging standard C library routines
- From: Eric Sosman
- 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
- Debugging standard C library routines
- Prev by Date: Re: Debugging standard C library routines
- Next by Date: Re: Malloc and free questions - learner questions
- Previous by thread: Re: Debugging standard C library routines
- Next by thread: Re: Debugging standard C library routines
- Index(es):
Relevant Pages
|