ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/10/14/09:03:49

Date: Sat, 14 Oct 1995 13:00:41 +0000
From: IBBT0 AT cc DOT uab DOT es
Subject: realloc-free question (right version!)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Organization: Universitat Autonoma de Barcelona

(Sorry for the 2 first incomplete versions. We have had a problem
with our mail program).

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=
=20
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 =3D malloc(FIRST_BLOCK))=3D=3DNULL)
{
  puts ("\n\aError 1");
  exit(1);
}

puts ("\nMemory 1 Ok. Filling with 'A'...");
nomfi2 =3D nomfitx;
for (i=3D0; i< FIRST_BLOCK; i++)
  nomfi2[i] =3D 'A';


if ((nomfitx =3D realloc (nomfitx, MAXLONG))=3D=3DNULL)
  puts ("\n\aError 2");

puts ("Reading A...");
for (i=3D0; i< FIRST_BLOCK; i++)
  printf ("%c ", nomfi2[i]);


puts ("Filling with 'B'...");
for (i=3D0; i< FIRST_BLOCK; i++)
  nomfi2[i] =3D 'B';

puts ("Reading B...");
getchar();

for (i=3D0; i< FIRST_BLOCK; i++)
  printf ("%c ", nomfi2[i]);

return 0;
}
***** END OF CODE ****



********************
If the answer is NO,
********************
it seems to me a problem with the C language, because there is no way=
 to
free this memory block unless you save the original pointer. In other
words, you lose the control over the memory block pointed by
"original_pointer":

=09char * original_pointer, * security_pointer;
=09=09:
=09=09:
=09original_pointer =3D malloc(some_size_available);
=09=09:
=09=09:
=09original_pointer =3D realloc(original_pointer, some_size_too_big);
=09
Now the original_pointer is NULL; so the sentence:

=09free (original_pointer);

is erroneous, but if I do not free "original_pointer", I have the blo=
ck of
"some_size_available" memory collapsed until the program ends.

The only way to free that memory is doing:

=09security_pointer =3D original_pointer;

after the malloc() call, and, after the non-successful realloc call, =
to do:

=09free (security_pointer);

**************

Thank you for your answer.


Xavier Pons and Joan Maso.=09
Centre de Recerca Ecologia i Aplicacions Forestals, CREAF, i
Unitat de Botanica. Facultat de Ciencies.
Universitat Autonoma de Barcelona, UAB.
08193. Bellaterra. Catalonia. Spain

  _ -
 ( ~)^))        Fax.=09+34-3-581.13.12        =20
( ( ~=B7)&)       E-mail=09ibbt0 AT cc DOT uab DOT es
 (\ ~)=B7,
   |=B7|/
   | |
___| |___

- Raw text -


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