realloc

Guy Harris guy at auspex.UUCP
Fri Mar 31 08:54:52 AEST 1989


>I'll say it's well-kept, neither the 4.2BSD nor SysV.2 manpages imply
>that calling realloc with first argument of 0 will do anything useful,
>let alone that it's *supposed* to.  The SunOS 4.0.0 manpage even says
>that "[realloc will fail if] an invalid argument was specified.

Yes.  However, the December 7, 1988 dpANS says:

	4.10.3.4 The "realloc" function

	Synopsis

		#include <stdlib.h>
		void *realloc(void *ptr, size_t size);

	Description

	  ... If "ptr" is a null pointer, the "realloc" function behaves
	like the "malloc" function for the specified size.

So, while the "well-kept secret" was, indeed, a secret in earlier
systems - since *no* promise about the behavior was listed in the
documentation - it's not likely to be a secret in ANSI C, unless that bit
gets deleted before the standard is issued.

So basically:

	1) if you know you'll only be running in ANSI C environments,
	   feel free to pass NULL as the first argument to "realloc"
	   (assuming that, in that case, you expect it to do a "malloc");

	2) if you *don't* know that, make sure all the assumptions you
	   made about your environment are backed up by the
	   documentation before you port to a new environment.



More information about the Comp.lang.c mailing list