9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Douglas A. Gwyn" <DAGwyn@null.net>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] manual suggestions and upas/fs bug
Date: Tue, 17 Sep 2002 08:47:45 +0000	[thread overview]
Message-ID: <3D8642ED.6BE57EE6@null.net> (raw)
In-Reply-To: <c881c5b5ee53a0c999a0f53343a87bf1@cs.cmu.edu>

David Swasey wrote:
> The malloc(2) page says:
>         The call realloc(0, size) means the same as `malloc(size)'.
>         Further, the call realloc(ptr, 0) means the same as
>         `free(ptr)'.
> I suggest you clarify the meaning of realloc(0,0).

If the intention is to be standard-conforming, then realloc(0,x)
is the same as malloc(x) for all x.  malloc(0) is required to
return either a null pointer or a valid heap pointer (which a
strictly conforming program is not allowed to dereference).

realloc(p,0) cannot be identical to free(p) since the former
returns a value and the latter does not.  Presumably it acts
as free(p) then does not succeed in allocating a 0-sized new
object, thus returns a null pointer.

free(0) is required to be a no-op.

> diff /n/dump/2002/0916/sys/src/cmd/upas/fs/mbox.c mbox.c
> 1358c1358,1362
> <       p = realloc(p, n);
> ---
> >       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).


  parent reply	other threads:[~2002-09-17  8:47 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 [this message]
2002-09-17  9:17   ` Lucio De Re
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=3D8642ED.6BE57EE6@null.net \
    --to=dagwyn@null.net \
    --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).