malloc(0)

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Aug 3 13:17:14 AEST 1990


In article <1990Jul31.222850.1873 at oresoft.com> dan at oresoft.com (Daniel Elbaum) writes:
>If, on the other hand, two pointers are obtained from malloc2(0), their
>values are known to be identical.  They compare equal, but it's impossible
>to say that they point to the same object, because what they point to isn't,
>formally speaking, an object.

It is, if the implementation provides this 0-sized object extension.

>In short, the second approach has some merit.  How (and why) is it
>forbidden by the Standard?

A strictly conforming program may not rely on malloc() supporting
0-sized objects at all.  If some implementation provides such
support as an extension, it should do so in a way that is consistent
with the principles used in the standard.  In this context, the most
important principles are that pointers to distinct objects must not
compare equal, and that each invocation of malloc() must allocate an
object distinct from all other currently valid objects.

I provide this just as guidance for implementors.  Technically, such
extensions are constrained by the standard only to the extent that
they can be interpreted in terms of the basic standard.  Since there
are no explicit guidelines for 0-sized objects in the standard, some
attributes of such objects in an extended implementation are subject
to "judgement calls".  As Point Of Contact for 0-sized objects, I've
heard essentially all of the arguments pro and con each choice, and
what I recommend is simply my best judgement concerning the matter.
In particular, if programs are to make real use of 0-sized objects,
they will benefit greatly if separately-malloc()ed ones do not seem
to have equal pointers.  Try writing some heavy-duty applications
involving this stuff and see what I mean.



More information about the Comp.std.c mailing list