Xref: utzoo comp.std.c:1007 comp.lang.c:17409 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.std.c,comp.lang.c Subject: Re: realloc Message-ID: <3255@goofy.megatest.UUCP> Date: 4 Apr 89 05:58:47 GMT References: <9132@alice.UUCP> Organization: Megatest Corporation, San Jose, Ca Lines: 35 From article <9132@alice.UUCP>, by andrew@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!