delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/22/22:29:55

Message-Id: <199912230049.SAA09717@lakdiva.slt.lk>
From: "Kalum Somaratna aka Grendel" <kalum AT newmail DOT net>
To: djgpp AT delorie DOT com
Date: Thu, 23 Dec 1999 06:51:41 +0600
MIME-Version: 1.0
Subject: Re: freeing memory and exiting (safely)
In-reply-to: <83ril4$4ua$1@portraits.wsisiz.edu.pl>
X-mailer: Pegasus Mail for Win32 (v3.12)
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 22 Dec 99, at 16:45, Chaos wrote:

> Hi there.
> 
> In my program, I use several buffers which are dynamically allocated
> (malloc) and also freed during run time (free). For every buffer I'm setting
> condition which looks close to this:
> 
> background=malloc(BACKGROUND_BUF_SIZE);
> if(!background)
>    {
>     fprintf(stderr,"Can't allocate memory for buffor ''background''! \n");
>     return 1;
>    }
> 
> My question is that if the prog reaches that "return 1" (synonim for error)
> does those buffers will be safely deallocated?

Hi again Chaos,

When your program terminates execution the OS (cwsdpmi or 
Windoze etc) should handle all the deallocation of the allocated 
memory used by that program.

So if that "return 1 " statement causes your program to terminate 
(ie if it's in the main() function) and exit to the OS  then you can 
count on all your previously undeallocated memory being freed.

So IMHO say if you allocate a 4M buffer and exit to the OS without 
freeing it the OS should free it and all the other memory that you 
process consumed. 

However IMHO it might be nice (at least for aesthetic purposes) to 
write a error handling function which in your case would cleanup 
the deallocating of previously allocated memory and do other things 
like restoring hooked interrupt's etc.

> I know I want something very weird ;-)
No way! It's not weird at all.

Kalum


- Raw text -


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