🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2978

Re: malloc(0)

#2978
From: Jos Horsmeier
Date: Sat, 21 Nov 1992 16:20
41 lines
1963 bytes
In article <1992Nov21.005207.9741@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
|The standard says  (refering to allocation routines)
|
|	If the size of the space requested is zero, the behavior
|	is implementation-defined; the value returned shall be
|	either a null pointer or a unique pointer.
|
|I have always read "unique pointer" as meaning different from
|previously allocated pointers including pointers previously
|returned by malloc(0).  However I have just been talking to
|some people who insist it means that the same pointer is returned
|on each call of malloc(0).
|
|Comments?

Quite an interesting question ... the answer all depends on the meaning
of the word `unique'. Consider the following snippet of code:

char *p, *q;

p= malloc(n);
q= malloc(n);

if (p == q) /* etc */

The condition always fails if malloc succeeds. If malloc(0) returns
a non NULL value (i.e. it doesn't fail) it should return a unique
pointer value every time it is invoked as malloc(0). On the other
hand, suppose malloc(0) returns NULL. The test would (obviously)
succeed. Because we don't want to break the code, when porting it
from a non-NULL returning implementation to a NULL returning imple-
mentation, this tends me to believe that malloc(0) should return the
same value over and over again, but that value must be different
from any other malloc(n) call, where n is non zero. But on the 
other hand, this would violate the constraint that the pointer
values `shall be unique.' The ISO standard doesn't have a rationale
included, so I can't say much more about this ...

kind regards,

Jos aka jos@and.nl
Technical Details
Message-ID: <3904@dozo.and.nl>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!and!jos
References: <1ejhrnINN5it@ftp.UU.NET> <1992Nov21.005207.9741@lucid.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sun4nl!and!jos
From: jos@and.nl (Jos Horsmeier)
Newsgroups: comp.std.c
Subject: Re: malloc(0)
Message-ID: <3904@dozo.and.nl>
Date: 21 Nov 92 16:20:34 GMT
References: <1ejhrnINN5it@ftp.UU.NET> <1992Nov21.005207.9741@lucid.com>
Organization: AND Software BV Rotterdam
Lines: 41