Date: Sat, 14 Oct 1995 12:53:33 +0000 From: IBBT0 AT cc DOT uab DOT es Subject: realloc-free question To: djgpp AT sun DOT soe DOT clarkson DOT edu Organization: Universitat Autonoma de Barcelona Dear programmers, When realloc returns NULL, it really frees the memory block that the pointer originally had? The knowledge of the right answer is very important for the memory management in C. ******************** If the answer is YES, ******************** The following code, compiled with DJGPP should produce a protection fault, but it does not (it seems to run perfectly): ***** BEGIN OF CODE **** #include #include #include int main (void) { char * nomfitx; char * nomfi2; unsigned long i; puts ("\n\n\nSTARTING:"); #define FIRST_BLOCK 1000000L if ((nomfitx = malloc(FIRST_BLOCK))==NULL) { puts ("\n\aError 1"); exit(1); } puts ("\nMemory 1 Ok. Filling with 'A'..."); nomfi2 = nomfitx; for (i=0; i< FIRST_BLOCK; i++) nomfi2[i] = 'A'; if ((nomfitx = realloc (nomfitx, MAXLONG))==NULL) puts ("\n\aError 2"); puts ("Reading A..."); for (i=0; i< FIRST_BLOCK; i++) printf ("%c ", nomfi2[i]); puts ("Filling with 'B'..."); for (i=0; i< FIRST_BLOCK; i++) nomfi2[i] = 'B'; puts ("Reading B..."); getchar(); for (i=0; i< FIRST_BLOCK; i++) printf ("%c ", nomfi2[i]); return 0; }