Re: free()



Christopher Benson-Manica said:

Ian Collins <ian-news@xxxxxxxxxxx> wrote:

Richard Bos wrote:

The problem with that is "attempting". You will, sooner or later,
fail in your attempt. And then, because you believe that all your
pointers are either null or valid, you will not be able to find the
bug until

Can you cite an example where setting a pointer to NULL could mask a
bug? The way I see it is there are three things you can do with a
pointer, test it, dereference it and free it. Once set to NULL, the
first to will fail and the third is harmless.

You missed Richard's point. The problem lies not in setting the
pointer to NULL, but in *believing* one has when in fact one has not.

Lots of problems can arise from believing one has done something when in
fact one has not. That doesn't mean that the something is a bad thing
to do.

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: Code fails with Segmentation Fault
    ... You haven't quite understood that creating a pointer ... understand that either one can fail. ... You should be aware that strdup() is not a Standard C ... you don't want a memory region ...
    (comp.lang.c)
  • Re: Help. What is the error?
    ... > The code will work if the returned pointer lies in the first 4GB ... int, void*, and int* are all 64 bits. ... It could either succeed or fail ... Undefined behavior is undefined behavior. ...
    (comp.lang.c)
  • Re: pthread_join SEGFAULT
    ... it is written that the function shall fail if the implementation has detected that the value specified by "thread" does not refer to a joinable thread. ... On my system if I call pthread_join; so with a non joinable thread "0", it triggers a SEGFAULT instead of safely returning with the error code EINVAL. ... You cannot legally assume anything about the representation of a pthread_t; it is an opaque value and might, for example, be a pointer to some internal implementation data. ...
    (comp.programming.threads)
  • Re: AFL (Australian Football League)
    ... 'You fail to check whether this call succeeded - fopen returns a null ... pointer if it cannot open the file. ... The fscanf function yields ... conversion) or 0 (if, say, the input doesn't make sense to fscanf in the ...
    (comp.lang.c)
  • Re: Kitl cannot connect
    ... The function call, HDConnectClient, returns 0, causing the connect to ... fail. ... a pointer to BOOL KdInit(HDSTUB_DATA *pHdData, ...
    (microsoft.public.windowsce.platbuilder)