[comp.lang.c] What happens when realloc

tom@yang.cpac.washington.edu (Tom May) (08/31/89)

If I do this:

	char *p1, *p2;
	p1 = malloc (100);
	p2 = realloc (p1, 200);

and malloc() returns non-NULL, but realloc() returns NULL, is the memory
pointed to by p1 still allocated?


--
Tom May
tom@yin.cpac.washington.edu

dfp@cbnewsl.ATT.COM (david.f.prosser) (08/31/89)

In article <TOM.89Aug30231415@yang.cpac.washington.edu> tom@yang.cpac.washington.edu (Tom May) writes:
>If I do this:
>	char *p1, *p2;
>	p1 = malloc (100);
>	p2 = realloc (p1, 200);
>and malloc() returns non-NULL, but realloc() returns NULL, is the memory
>pointed to by p1 still allocated?

Section 4.10.3.4 (the realloc function), page 156, lines 23-24:

	"If the space cannot be allocated, the object pointed to by ptr
	[the first parameter] is unchanged."

Dave Prosser	...not an official X3J11 answer...

cpcahil@virtech.UUCP (Conor P. Cahill) (08/31/89)

In article <TOM.89Aug30231415@yang.cpac.washington.edu>, tom@yang.cpac.washington.edu (Tom May) writes:
> and malloc() returns non-NULL, but realloc() returns NULL, is the memory
> pointed to by p1 still allocated?

This is yet another one of those RTFMs.  The malloc page (under
the DIAGNOSTICS section) clearly states the following:

System V Rel 3.x:	When realloc returns NULL, the block pointed to by
       &		ptr is left intact.
SCO Xenix 2.3.2


System V Rel 2.x:	When this happens the block pointed to by ptr may
       			be destroyed.

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/01/89)

In article <TOM.89Aug30231415@yang.cpac.washington.edu> tom@yang.cpac.washington.edu (Tom May) writes:
>	p2 = realloc (p1, 200);
>... realloc() returns NULL, is the memory pointed to by p1 still allocated?

Yes, a properly implemented realloc() will not lose the old storage when
it cannot perform the reallocation.

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/01/89)

In article <1103@virtech.UUCP> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>System V Rel 2.x:	When this happens the block pointed to by ptr may
>       			be destroyed.

The SVID Issue 2 does not permit that, though, and SVR2.0 in fact does
not overwrite the storage.  However, the block at ptr is marked as
being free, which makes it tricky to recover, since any further use of
malloc() or realloc() might step on the freed data.  Although the C
Standard is not crystal-clear on this point, the intent is that the
block is supposed to still be allocated.

This was (correctly) noted as a "BUG" in some manual pages.

andre@targon.UUCP (andre) (09/01/89)

In article <TOM.89Aug30231415@yang.cpac.washington.edu> tom@yang.cpac.washington.edu (Tom May) writes:

>and malloc() returns non-NULL, but realloc() returns NULL, is the memory
>pointed to by p1 still allocated?

Yes, it is or nobody would use realloc anymore... :-)

-- 
    \---|    AAA         DDDD  It's not the kill, but the thrill of the chase.
     \  |   AA AAvv   vvDD  DD        Ketchup is a vegetable.
  /\  \ |  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
_/__\__\| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre