Re: free()



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.

.



Relevant Pages

  • Re: free()
    ... where the assignment of NULL is completely pointless. ... It frees the store its argument points to and names what's ... other circumstances where you need to destroy a trollgun other than the ...
    (comp.lang.c)
  • Re: Integer Promotions
    ... No, I disagree. ... assignment expression, because the assignment itself was the purpose of ... Casting the assignment to void, however, is pointless. ... necessary to do this in order to discard the value. ...
    (comp.lang.c)
  • Re: Incrementing in C
    ... Calling `foo` doesn't change that. ... Because the assignment of `y` to `x` is pointless; ... Chris "echo echo" Dollin ...
    (comp.lang.c)
  • Re: My Complaint about Dave Moore
    ... Pointless point Greg. ... Did you wake up on the wrong side ... of the rock this morning? ...
    (alt.guitar.amps)