Mail Archives: djgpp/1995/10/14/08:48:49
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 <stdio.h>
#include <stdlib.h>
#include <values.h>
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;
}
- Raw text -