malloc(0)

Daniel Elbaum dan at oresoft.com
Wed Aug 1 08:28:50 AEST 1990


In <13441 at smoke.BRL.MIL> gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
:In article <1990Jul26.232216.15093 at oresoft.com> dan at oresoft.com (Daniel Elbaum) writes:
:>Is this meaning of "unique" consistent with the wording and intent of 4.10.3?

:Unique pointers mean just what I said in my previous message.
:I don't know why you're concentrating on the (empty) content of the objects;
:I was discussing pointers to the objects, which is what matters here.
:I could repeat what I said before, or you could retrieve it and study
:it until you understand my point.

Your point isn't difficult to understand.  But it was stated as an
assertion rather than a conclusion.  You said

	The clear intent of the pointer uniqueness requirement is to
	ensure that equality of valid pointers implies that the same
	object is pointed to by both pointers.  If it were not for this
	requirement, all malloc(0) requests could be satisfied by
	returning a constant pointer to some valid library object.

I meant to ask what you mean by "the same object."  Suppose we have
two library routines, malloc1() and malloc2().  Malloc1 conforms to
the requirements you mentioned in your previous posting.  Malloc2
behaves like the hypothetical routine suggested in Bothner's
original article--it always returns the same valid pointer when
called with a zero argument, and never returns that pointer when
called with a nonzero argument.  Now if two valid pointers are
obtained from malloc1(0) (with no intervening call to free()),
the pointers are known to be distinct.  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.

Leaving aside the issue of ease of implementation, each approach has
one major advantage.  Malloc1 provides a safe means of obtaining a
unique, valid pointer.  Malloc2 provides a single value for the
pointer-to-zero-sized-object.  Either may be useful according to
the application.  For example, one approach to error handling might
take advantage of malloc2's behavior by checking pointers against
the "known-zero" pointer at critical spots in a program.

In short, the second approach has some merit.  How (and why) is it
forbidden by the Standard?
-- 
Being conservative in most respects and reluctant to transform country and
constitution, the coup d'etat typically neither attracts nor needs the fanatic.

({uunet,tektronix,reed,sun!nosun,m2xenix}!oresoft!(dan)@oresoft.com)
-- 
Being conservative in most respects and reluctant to transform country and
constitution, the coup d'etat typically neither attracts nor needs the fanatic.

({uunet,tektronix,reed,sun!nosun,m2xenix}!oresoft!(dan)@oresoft.com)



More information about the Comp.std.c mailing list