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: Kevin Buettner <kevinb at cygnus dot com>
- Subject: Re: xfree() -- set ptr to nil (fwd)
- From: "John R. Moore" <jmoore at cygnus dot com>
- Date: Mon, 12 Feb 2001 15:33:28 -0800 (PST)
- cc: <gdb-patches at sources dot redhat dot com>
Yes, I've usually seen this as a macro
#define XFREE(ptr) do \
{ \
if (ptr) \
{ \
free (ptr); \
ptr = NULL; \
} \
} while (0)
Ok, then, do we want to replace xfree() with something like XFREE() ?
John (Can I hide my first post? :-) )
On Mon, 12 Feb 2001, Kevin Buettner wrote:
> On Feb 12, 3:07pm, John R. Moore wrote:
>
> > Whilst fixing xfree() callsI noticed that xfree() itself has a peculiarity
> > that needs attention:
> >
> > The call goes like this:
> >
> > if (ptr != NULL)
> > free(ptr);
> >
> > Nice, but why not the following:
> >
> > if (ptr)
> > {
> > free (ptr);
> > prt = NULL);
> > }
> >
> > The latter catches any re-calls to xfree(), unless the compiler sets the
> > ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
> > to do this anyhow.
> >
> > Any opinions? The only reason I can think not to is to insure that gdb
> > core dumps on succesive xfree() calls to the same pointer (and hence
> > insure efficient code, but in that case, why bother with xfree() in the
> > first place.
>
> Let me see if I understand you correctly. You'd like to replace
>
> void
> xfree (void *ptr)
> {
> if (ptr != NULL)
> free (ptr);
> }
>
> with
>
> void
> xfree (void *ptr)
> {
> if (ptr)
> {
> free (ptr);
> ptr = NULL;
> }
> }
>
> right?
>
> If so, how will this work? ``ptr'' is a local variable and will not
> be modified outside the scope of xfree().
>
> What you have in mind could be done with a macro and I have seen
> this done in other programs. (But rather than insuring that gdb
> core dumps on successive xfree() calls, it instead causes gdb to
> core dump when attempting to use an already freed-and-nulled pointer.)
>
> Kevin
>