9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Lucio De Re <lucio@proxima.alt.za>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] manual suggestions and upas/fs bug
Date: Tue, 17 Sep 2002 11:17:27 +0200	[thread overview]
Message-ID: <20020917111727.U6063@cackle.proxima.alt.za> (raw)
In-Reply-To: <3D8642ED.6BE57EE6@null.net>; from Douglas A. Gwyn on Tue, Sep 17, 2002 at 08:47:45AM +0000

On Tue, Sep 17, 2002 at 08:47:45AM +0000, Douglas A. Gwyn wrote:
> > >       if(n==0){
> > >               free(p);
> > >               p = malloc(n);
> > >       } else
> > >               p = realloc(p, n);
>
> p = realloc(p,n) is almost always a mistake.  If the allocation
> fails, the previous pointer value in p gets replaced by a null
> pointer value, so you lose the handle to the data that did not
> get extended (or, rarely, shrunk).

In other words,

	if (n==0)
		panic ("zero size");
	p = realloc(p, n);

is closer to the intent here.  Better still

	char *p0 = realloc(p, n);

	if (p0 == 0)
		panic ("realloc failed");
	p = p0;

would be a lot saner.  I forget the semantics, but realloc(p, n) need
not return p even if the reallocation succeeds.  What I don't know is
what happens to the space pointed to by p, I assume it's released.

++L


  reply	other threads:[~2002-09-17  9:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-16 19:29 David Swasey
2002-09-17  1:58 ` Lyndon Nerenberg
2002-09-17  8:47 ` Douglas A. Gwyn
2002-09-17  9:17   ` Lucio De Re [this message]
2002-09-18  8:37     ` Douglas A. Gwyn
2002-09-17  4:34 David Swasey
2002-09-17 20:18 ` Lyndon Nerenberg
2002-09-17  4:58 Russ Cox
2002-09-17 13:28 Russ Cox
2002-09-17 23:05 Russ Cox
2002-09-18  2:06 ` Lyndon Nerenberg
2002-09-18 17:03   ` rob pike, esq.

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=20020917111727.U6063@cackle.proxima.alt.za \
    --to=lucio@proxima.alt.za \
    --cc=9fans@cse.psu.edu \
    /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;
as well as URLs for NNTP newsgroup(s).