realloc
Dave Jones
djones at megatest.UUCP
Tue Apr 4 15:58:47 AEST 1989
>From article <9132 at alice.UUCP>, by andrew at alice.UUCP (Andrew Hume):
> what is
> all the whining about? can't malloc(0) return a pointer to 1 (or 2 or 4)
> bytes? of course, everyone would be happy now, wouldn't they?
Hear! Hear!
Indeed, most implementations could actually return a valid pointer to
ZERO bytes, if they wanted to!
Yep. Typically a pointer to a heap-packet is the machine-address of the
memory just after a heap-packet HEADER, which the heap routines use
to keep books. Since no two heap-packets have the same headers, no
two heap-packets have the same heap-packet pointer. This remains
true even if no memory beyond the header is actually reserved.
In practice, a heap-packet-pointer-to-nothing would typically
be the machine address of another heap-packet-header. We dare not
dereference it, but so what? It is always an error to use a
heap-packet-pointer to index as much as heap-packet-size bytes into the
heap-packet. (Doing so usually gets you into another heap-packet-header,
or another heap-packet, and can keep you awake debugging all night
and most of the next day, so don't DO that!)
Ego, with a pointer to zero bytes, it is an error to index even zero-bytes
beyond it. That is to say, the pointer-to-nothing is good only for
comparing for equality with similarly typed pointers.
No problem.
Maybe on a Prevert-2000, that keeps different kinds of pointers in
different shapes of mayonaise jars, you would have to allocate a byte
or four of memory. Okay. Fine.
Ain' no deal, Home!
More information about the Comp.lang.c
mailing list