What if I don't free() all my dynamically allocated memory?

Stephen M. Dunn cs3b3aj at maccs.McMaster.CA
Sat Apr 8 12:44:50 AEST 1989


In article <2580 at ssc-vax.UUCP> dmg at ssc-vax.UUCP (David Geary) writes:
>  Anyway, I'm wondering if it's ok for me to just leave the freeing
>  out altogether.  Unix will free all of my resources for me anyway
>  right?  Is there any problem with doing this?
>
>  Of course, I realize this would cause serious problems if ever
>  ported to a lowly PC, but I don't care ;-)

   Actually, this may be ok on a PC, too.  I don't know how the typical
C compiler on a PC handles memory allocation, but I would suspect it's
similar to Turbo Pascal:  when the program starts, it grabs all available
memory and then dishes it out to the program using its own memory manager.
Why?  Because MS-DOS will only allow you to allocate memory in 16-byte
chunks, while your program may try to allocate (say) three bytes at a time,
leading to a colossal waste of memory.  By grabbing all memory and handling
allocation itself, an executable can use memory much more space-efficiently
(although this approach is incompatible with multi-tasking operating systems).

   When the program terminates, the memory-management routines built into
your executable by the compiler simply return all the memory to DOS, so
your program _must_ free its allocated memory only if it will run out of
memory if it doesn't.  Neat, huh?

   Oh, and to overcome the problem with multi-tasking, the compiler will
most likely allow you to set an option limiting the maximum amount of memory
it grabs.  For more complicated programs, however, the analysis required to
estimate a realistic figure for this becomes astronomical.

   As I said, I've never looked to see if compilers other than Turbo Pascal
(3 and 4, and probably 5, too) do this, I would think it likely that they
do.

   Anyway, just thought you might like to know the "lowly" PC might even
be able to handle your code.

Regards

-- 
======================================================================
! Stephen M. Dunn, cs3b3aj at maccs.McMaster.CA ! DISCLAIMER:           !
! This space left unintentionally blank - vi ! I'm only an undergrad !
======================================================================



More information about the Comp.lang.c mailing list