realloc and malloc and zero-sized objects
Larry Jones
scjones at sdrc.UUCP
Thu Apr 6 07:06:35 AEST 1989
In article <1989Apr5.000608.16582 at sq.com>, msb at sq.com (Mark Brader) writes:
> [Much interesting text deleted - see original if interester]
>
> Since 0 is not a possible size for an object, the invocation malloc(0)
> falls under the general rule [for out-of-range arguments to
> library functions] and is undefined behavior. It would of
> course be permissible for an implementation to define its behavior in
> this circumstance; this would be an extension to the language.
This would be true except for the statement in 4.10.3 Memory
Management Functions:
"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."
Thus, even though there ain't no such thing as a zero sized
object, malloc and friends are allowed to return non-null
pointers when given a size of zero.
> # Returns
> # The realloc function returns either a null pointer or a pointer to the
> # possibly moved allocated space.
>
> Note the either-or. Since a null pointer cannot point to allocated space,
> this is really an exclusive or. Since an object cannot have zero size
> and also because of the last sentence in the Description section, it is
> clear that realloc is *not* allocating any space if it is called as
> realloc(p,0) where p is a valid non-null pointer. Therefore realloc *must*
> return a null pointer.
But the statement above gives realloc the liberty to allocate
space if it wants to, so whether it returns a null pointer or not
is implementation defined as I said originally.
----
Larry Jones UUCP: uunet!sdrc!scjones
SDRC scjones at sdrc.UU.NET
2000 Eastman Dr. BIX: ltl
Milford, OH 45150 AT&T: (513) 576-2070
"When all else fails, read the directions."
More information about the Comp.std.c
mailing list