www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/10/14/08:48:33

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 <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 -


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