www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/28/01:31:58

From: edc AT arches DOT uga DOT edu (Edward Cashin)
Newsgroups: comp.os.msdos.djgpp
Subject: djgpp malloc and freeing memory on exit
Date: 28 Mar 1998 04:25:43 GMT
Organization: The University of Georgia
Lines: 29
Message-ID: <6fhu47$n1i$1@cronkite.cc.uga.edu>
NNTP-Posting-Host: arch13.cc.uga.edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello.  I hear continually that it is good 
programming practice to have a call to free for every call to 
malloc.  It certainly makes sense to free memory once it will 
no longer be used in the program.  However, there will be cases 
in which dynamically-allocated memory doesn't need to be freed 
until the program exits.

I have not had trouble so far exiting without freeing memory
allocated with a call to malloc, and AFAIK that is because W95
has been nice, freeing up all the memory after my program exits.
I could be wrong: maybe djgpp's exit routines take care of it.

If the latter is the case, I wouldn't waste time writing code
to track and free memory at program exit.  If W95 is in charge
of the cleanup, then I'd rather do it myself...how's this for a
way to do that:

A function called mem_mgr() would be informed when memory was 
allocated with malloc, and it would store the pointer to the 
malloc'ed memory.

A function called void clear_mem(void) would be set to be called
at exit.  clear_mem() would call mem_mgr() in such a way that
mem_mgr() would run through all the pointers it has stored,
making a call to free for each.

=====================
Ed edc AT arches DOT uga DOT edu
=====================

- Raw text -


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