Re: free()
- From: Chris Dollin <chris.dollin@xxxxxx>
- Date: Wed, 14 Feb 2007 11:24:35 +0000
Richard Heathfield wrote:
Chris Dollin said:
Beej wrote:
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.
It frees the store its argument points to and names what's
going on, so the /function/ isn't pointless.
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 */
}
}
}
Interesting. I'm trying to articulate why I find that overkill.
I /think/ it's that where I free something, either the place
I'm freeing it from is about to evaporate (as in your horn,
leg, and trollgun), so the assignment doesn't help, or I'm
about to assign a new value to that place, ditto. Probably because
we're writing different kinds of code. With a shared goal:
"don't let invalid pointers screw you up - get rid of them PDQ".
--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.
.
- Follow-Ups:
- Re: free()
- From: Richard Heathfield
- 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
- Re: free()
- From: Richard Heathfield
- 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
|