Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@cygnus.com>
To: "John R. Moore" <jmoore@cygnus.com>, <gdb-patches@sources.redhat.com>
Subject: Re: xfree() -- set ptr to nil (fwd)
Date: Mon, 12 Feb 2001 15:21:00 -0000	[thread overview]
Message-ID: <1010212232133.ZM10698@ocotillo.lan> (raw)
In-Reply-To: <Pine.GSO.4.31.0102121506170.16672-100000@cse.cygnus.com>

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


  reply	other threads:[~2001-02-12 15:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-12 15:07 John R. Moore
2001-02-12 15:21 ` Kevin Buettner [this message]
2001-02-12 15:33   ` John R. Moore
2001-02-12 15:46     ` Daniel Berlin
2001-02-12 15:52       ` John R. Moore
2001-02-12 15:54     ` J.T. Conklin
2001-02-12 15:25 ` J.T. Conklin
2001-02-12 16:01 Michael Elizabeth Chastain
2001-02-13 14:12 ` Andrew Cagney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1010212232133.ZM10698@ocotillo.lan \
    --to=kevinb@cygnus.com \
    --cc=gdb-patches@sources.redhat.com \
    --cc=jmoore@cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox