www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/24/07:55:32

From: loth AT cow-net DOT com (Burton Radons)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Is realloced memory copied when necessary?
Date: Sun, 23 May 1999 23:08:19 GMT
Organization: Posted via RemarQ, http://www.remarQ.com - Discussions start here!
Lines: 34
Message-ID: <374988d5.62603017@news.cow-net.com>
References: <7i7sn2$uvt$2 AT news5 DOT svr DOT pol DOT co DOT uk>
NNTP-Posting-Host: 207.107.185.156
Mime-Version: 1.0
X-Trace: 927500768 YR/AJA.QCB99CCF6BC usenet58.supernews.com
X-Complaints-To: newsabuse AT remarQ DOT com
X-Newsreader: Forte Agent 1.5/32.452
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sun, 23 May 1999 04:29:46 +0100, "Andrew Davidson"
<andrew AT lemure DOT freeserve DOT co DOT uk> wrote:

>I need to know if, on making a call to realloc to expand the size of some
>memory I am using, will realloc copy the data from the original memory I
>pass to it if it returns a different pointer, or will I have to do that
>myself?

reallocing to new memory implies that the previous memory has been
free'd.  Since it isn't correct to access free'd memory even the line
after you free'd it, if realloc didn't copy memory you would have no
way of getting the original memory over.

This would have been trivial to test.

#include <stdlib.h>

int main (void)
	{
		int *ptr = malloc (sizeof (int)), *optr;
		if (ptr == NULL)
			return 3;
		*ptr = 4325;
		ptr = realloc ((optr = ptr), 32768);
		if (optr == ptr)
			return 2;
		if (*ptr != 4325)
			return 1;
		return 0;
	}

- Burton Radons, loth AT cow-net DOT com
Vancouver Island, British Columbia, Canada
(main) http://csoft.net/~loth/index.shtml

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019