From mboxrd@z Thu Jan 1 00:00:00 1970 From: jtc@redback.com (J.T. Conklin) To: "John R. Moore" Cc: Subject: Re: xfree() -- set ptr to nil (fwd) Date: Mon, 12 Feb 2001 15:25:00 -0000 Message-id: <5mzofr1o34.fsf@jtc.redback.com> References: X-SW-Source: 2001-02/msg00180.html >>>>> "John" == John R Moore 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