Re: free()



Richard Heathfield wrote:

Chris Dollin said:

<snip>

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.

Yes, likewise, which is why I write the destructor in the way I showed
you.

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.

No, there's another possibility - that the entity that had the gun no
longer has a gun (I dunno, perhaps the troll rips out the arm that was
holding it or something), and thus NULL is an appropriate value for
that pointer.

Yes - sometimes the new value is null.

Sometimes, rather than nulling the pointer because you're freeing it
(well, the pointed-to object), you're freeing the pointer (ditto)
because you're nulling it [1].

For me, this is often the case, (not that I use trollguns
very often) - objects gain and lose other objects with careful abandon.

Indeed.

[1] And you know this is the only surviving usable reference.

--
Chris "electric hedgehog" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

.



Relevant Pages

  • Re: double free
    ... >Does anyone know of a good website that actually describes and ... implementation dependent. ... Obviously the proper way to handle freeing a pointer more than once is ...
    (comp.lang.c)
  • Re: Free implementation in C
    ... Whenever we free memory using freefunction we set that memory to ... zero after freeing. ... // Set the pointer to the memory to a null pointer. ... Therefore, in order to use a nulling free(), you have to create two pointers; a void* that can be zeroed, and an int* that can be used. ...
    (comp.lang.c.moderated)
  • Re: free()
    ... thing, but also because it's actively obfuscating serious problems, ... If a pointer is null, you can free it as often as you like. ... freeing an invalid pointer has undefined and indeterminate results. ...
    (comp.lang.c)
  • Re: Quick free() question
    ... is freeing each pointer in the array sufficient or is it ... null the pointers in the array, then there is no more crash! ...
    (microsoft.public.vc.language)
  • Re: Why it is not good code for constructor
    ... How can C's destructor free the memory? ... responsible for freeing it, but C::~C will not be called, and a function-try ... handler cannot access the pointer, while a catch handler in local scope will ... now if new intcompletes ok, but the constructor for A throws, the ...
    (microsoft.public.vc.language)