realloc

Chris Torek chris at mimsy.UUCP
Mon Apr 3 10:17:38 AEST 1989


[flaming about realloc((char *)NULL) deleted]

In article <1196 at cmtl01.UUCP> oisin at cmtl01.UUCP (Oisin) writes:
>Pardon my ignorance, but if I'm wrong you can flame me too...
>Does the draft not specify that whatever implementation of NULL is used, 
>the compiler must guarantee that a ptr containing that implementation's
>version of NULL must test as false?

Yes.  In standard-ese, a null pointer of any type must compare equal
to the integer constant zero and to the nil-pointer-to-void ((void *)0).
This has nothing to do with the flaming about

	realloc((char *)NULL, sz),  where sz > 0

nor that about

	malloc((size_t)0)
and	realloc(ptr, (size_t)0)

(The `correct' set of definitions---which is not the same as the pANS
set, but is fairly close; I can deal with the pANS version---appears
to me to be:

	malloc(sz) should return a unique pointer to at least sz bytes,
	or the value (void *)NULL if space is not available

	free((char *)NULL) should be a no-op, not an error

	realloc(ptr, sz) should `act like' free(ptr)+malloc(sz)+copy(old
	data, new location).  In particular, realloc(NULL) should act
	like malloc, and realloc(?, 0) should return a unique pointer to
	at least zero bytes.

This could be done in an ANSI-conformant environment with a small set
of wrapper functions.  Note that, with these definitions, malloc()
becomes unnecessary; it need not appear as a wrapper function.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list