Re: free()
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 14 Feb 2007 11:10:04 +0000
Chris Dollin said:
Beej wrote:
On Feb 13, 1:38 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Short of writing an entire pointer management subsystem, there isn't
much you can do with a pointer to check it for validity, but you
*can* test it against NULL. If you know it's NULL, you know it's
invalid. If it isn't NULL, you can't be sure either way. So setting
it to NULL when it would otherwise be indeterminate is a Good Thing,
because it increases the amount of information available to you at a
trivial cost.
At last... this thread is finally starting to boil down to "NULL
makes a great sentinel value." ;)
-Beej
void delete_goat(GOAT *g)
{
free(g);
g = NULL;
}
This being a place where the assignment of NULL is completely
pointless.
....but then it's a pointless function anyway.
I write destructors like this:
void goat_delete(GOAT **g)
{
if(g != NULL)
{
if(*g != NULL)
{
GOAT *p = *g; /* purely for notational convenience */
horn_delete(&p->horn);
trollgun_delete(&p->trollgun);
leg_delete(&p->leg);
free(p);
*g = NULL; /* *not* a pointless assignment */
}
}
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: free()
- From: Chris Dollin
- Re: free()
- References:
- free()
- From: dbansal
- Re: free()
- From: santosh
- Re: free()
- From: Racaille
- Re: free()
- From: santosh
- Re: free()
- From: Richard Bos
- Re: free()
- From: Ian Collins
- Re: free()
- From: Christopher Benson-Manica
- Re: free()
- From: Richard Heathfield
- Re: free()
- From: Beej
- Re: free()
- From: Chris Dollin
- free()
- Prev by Date: Re: free()
- Next by Date: Re: free()
- Previous by thread: Re: free()
- Next by thread: Re: free()
- Index(es):
Relevant Pages
|