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

Paul Sander paul at athertn.Atherton.COM
Fri May 5 05:11:36 AEST 1989


In article <1384 at dukeac.UUCP>, bet at dukeac.UUCP (Bennett Todd) writes:
> In article <2580 at ssc-vax.UUCP| dmg at ssc-vax.UUCP (David Geary) writes:
>> [Complains that nearly half his time is spent executing free() when
>>  destroying trees of "generic" structures]
>
> [discusses guts of malloc()]
>                                                                   I think most
> OS memory management schemes look more like a stack than like a heap, but in
> any case, regardless of what malloc() is running on top of, the resources it
> allocates should be freed just fine upon exiting the program. If they weren't,
> many common programming errors would crash the system very quickly. 
> 
> I never bother calling free, unless I have a system that could be creating and
> tearing down variable sized objects all day long. If I am working with only a
> limited number of possible sizes, I make my own allocators that work with a
> linked free list for each type (grabbing more memory from malloc when
> necessary, in big blocks, and returning free memory to a linked list rather
> than the malloc/free heap). This runs extremely fast. If your program never
> needs to reuse memory, then you don't need to worry about freeing at all.

I once programmed in an environment that did NOT, repeat NOT, automatically
free allocated heap memory when a program terminated.  Specifically, it was
CICS running under VSE/Advanced Functions on an IBM 4341.  One could
allocate memory just fine, and could free memory just fine.  There was a
special function one could call that would free all heap memory allocated
up to that time, but it was broken.

Deciding goodness or badness is left as an exercise for the reader; my point
is that this environment exists, and if you want your code to port to it,
free() your stuff.  If free() doesn't perform, build your own heap manager
on top of it which is better suited to your structures.

Sorry about the terseness of this message, but this has been an "exciting" 
week.
-- 
Paul Sander        (408) 734-9822       | Do YOU get nervous when a
paul at Atherton.COM                       | sys{op,adm,prg,engr} says
{decwrl,sun,hplabs!hpda}!athertn!paul   | "oops..." ?



More information about the Comp.lang.c mailing list