This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
Re: xfree() -- set ptr to nil (fwd)
- To: "John R. Moore" <jmoore at cygnus dot com>
- Subject: Re: xfree() -- set ptr to nil (fwd)
- From: jtc at redback dot com (J.T. Conklin)
- Date: 12 Feb 2001 15:25:03 -0800
- Cc: <gdb-patches at sources dot redhat dot com>
- References: <Pine.GSO.4.31.0102121506170.16672-100000@cse.cygnus.com>
- Reply-To: jtc at redback dot com
>>>>> "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