This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: xfree() -- set ptr to nil (fwd)


>>>>> "John" == John R Moore <jmoore@cygnus.com> writes:
John> Nice, but why not the following:
John>
John> if (ptr)
John>   {
John>      free (ptr);
John>      prt = NULL);
John>   }
John>
John> The latter catches any re-calls to xfree(), unless the compiler sets the
John> ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
John> to do this anyhow.
John>
John> Any opinions?  The only reason I can think not to is to insure that gdb
John> core dumps on succesive xfree() calls to the same pointer (and hence
John> insure efficient code, but in that case, why bother with xfree() in the
John> first place.

This won't work.  Remember ptr is passed by value.  While you can
change ptr's value within xfree(), that doesn't change the value of
the variable that was passed to it.

IMO, xfree()'s reason for existance is not protecting against multiple
frees of the same object, but supporting frees of a possibly NULL ptr.
Modern free() implementations already support this, but we support old
pre-ANSI C libraries as well.  

        --jtc

-- 
J.T. Conklin
RedBack Networks


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]