realloc

Wonderly gregg at ihlpb.ATT.COM
Thu Mar 30 01:24:38 AEST 1989


>From article <10170 at bloom-beacon.MIT.EDU>, by scs at adam.pika.mit.edu (Steve Summit):
> Today, for the third time in half as many years, I got badly
> burned, and wasted lots of time, due to a bug in a certain
> vendor's implementation of realloc.  It is an apparently well-
> kept secret that realloc is supposed to behave gracefully at
> a slightly special-cased boundary point: when handed a NULL
> pointer and a nonzero size, it acts essentially as a malloc.

I read the man page for realloc and it said nothing about this.  Is it
not possible for you to type

	if (ptr == NULL)
		ptr = malloc (nbytes);
	else
		ptr = realloc (ptr, nbytes);

or are you one of those people that assumes (*((char *)NULL) == 0) too?
This damn lazy programming and sorry excuses for not being defensive has
got to stop.  Hiding all of the magic in the inards of the implementation
does not make it any easier for people to understand the code!  I have
never assumed that realloc() would accept a NULL pointer and if I ever
saw the type of coding that you talk about I would instantly say BUG.
Its like seeing "malloc (strlen (s));".  I know it is W-R-O-N-G!

-- 
Gregg Wonderly                             DOMAIN: gregg at ihlpb.att.com
AT&T Bell Laboratories                     UUCP:   att!ihlpb!gregg



More information about the Comp.std.c mailing list