Re: free()
- From: Chris Dollin <chris.dollin@xxxxxx>
- Date: Wed, 14 Feb 2007 11:45:40 +0000
Richard Heathfield wrote:
[some re-flow]
Chris Dollin said:
Richard Heathfield wrote:
Chris Dollin said:
Beej wrote:
void delete_goat(GOAT *g) { free(g); g = NULL; }
[...] 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.
free(whatever_you_would_have_passed_to_delete_goat); would do the same
trick, though - it frees the memory and (if the pointer is well-named)
documents what's going on as well, to anyone who knows what free() is
for - and if they don't know, why are we letting them touch production
code?
When goats are given additional pointer members (such as arms),
freeing a goat will have to deal with this. I find it easiest
to have a goatFree function right from the start, so that
I don't have to go free(aGoat)-hunting and change it. Calling
`free` directly risks forgetting to handle the insides ...
(destructor snipped)
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,
It might not only be goats that use trollguns, and anyway there may be
other circumstances where you need to destroy a trollgun other than the
one where you are destroying - sorry! deleting - a goat.
Yes - but the same argument applies: either I'm going to destroy
the place that pointed to the gun, so no point nulling it, or I'm
going to give it a new value, so no point nulling it. That's the way
my code seemed to turn out, anyway.
[I write very little C these days: s'mostly Java. It's amusing
how each language highlights issues with the other ...]
[...] 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".
Yeah. The thing is, different people think in different ways.
So true. Not all different ways are bonkers.
--
Chris "electric hedgehog" Dollin
"Who are you? What do you want?" /Babylon 5/
.
- Follow-Ups:
- Re: free()
- From: Beej
- 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
- Re: free()
- From: Chris Dollin
- Re: free()
- From: Richard Heathfield
- free()
- Prev by Date: Re: free()
- Next by Date: Re: Macro substitution: curly braces behaviour?
- Previous by thread: Re: free()
- Next by thread: Re: free()
- Index(es):
Relevant Pages
|