realloc

Barry Margolin barmar at think.com
Tue Apr 23 14:59:37 AEST 1991


In article <1991Apr23.001451.14900 at tkou02.enet.dec.com> diamond at jit345.enet@tkou02.enet.dec.com (Norman Diamond) writes:
>In article <91112.102011LSC at SLACVM.SLAC.STANFORD.EDU> LSC at SLACVM.SLAC.STANFORD.EDU writes:
>>     "If the space cannot be allocated, the object pointed to by ptr is unchang
>>unchanged."
>Failure is still indicated by returning a null pointer as the function's
>returned value.

What this *does* mean is that the common practice of

	fooptr = realloc (fooptr, newsize);

results in a memory leak whenever realloc() returns a null pointer.  Of
course, when realloc() fails, it's probably because you're out of memory,
so this one unfreeable allocation probably isn't a problem.  However, the
point is that one *should* write

	new_fooptr = realloc (fooptr, newsize);
	if (new_fooptr) {
	    fooptr = new_fooptr;
	}
	else /* report the error */
--
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.std.c mailing list