Re: free()



Beej wrote:

On Feb 13, 1:38 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Short of writing an entire pointer management subsystem, there isn't
much you can do with a pointer to check it for validity, but you *can*
test it against NULL. If you know it's NULL, you know it's invalid. If
it isn't NULL, you can't be sure either way. So setting it to NULL when
it would otherwise be indeterminate is a Good Thing, because it
increases the amount of information available to you at a trivial cost.

At last... this thread is finally starting to boil down to "NULL makes
a great sentinel value." ;)

-Beej

void delete_goat(GOAT *g)
{
free(g);
g = NULL;
}

This being a place where the assignment of NULL is completely
pointless.

--
Chris "electric hedgehog" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

.



Relevant Pages

  • Re: NULL with representation other then all bits 0
    ... Undefined Behaviour. ... Perhaps if you can show an example of "testing a pointer value for validity", I can understand why a null pointer value is special in that context. ... The test is valid because malloc will return NULL or a valid pointer ...
    (comp.lang.c)
  • Re: NULL with representation other then all bits 0
    ... accessing the value of ptr invokes undefined behavior if its ... >> No pointer value other than NULL can be tested for validity. ... non-NULL if malloc() succeeds). ...
    (comp.lang.c)
  • Re: NULL with representation other then all bits 0
    ... pointer arithmetic is defined only with a single object. ... And a null pointer is just a special representation (or ... use ptr with careless abandon .. ... You can't examine ptr to determine its validity. ...
    (comp.lang.c)
  • Re: [PATCH 3/4] map multiple blocks for mpage_readpages()
    ... Badari Pulavarty wrote: ... I wonder if we really need that map_valid pointer there. ... communicating the validity of a bh is via buffer_uptodate. ...
    (Linux-Kernel)