C
Chris Dollin
Richard said:[some re-flow]
Chris Dollin said:Richard said: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)
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.