Mail Archives: djgpp/1999/05/24/07:55:32
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 -