Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!decwrl!nsc!pyramid!athertn!paul From: paul@athertn.Atherton.COM (Paul Sander) Newsgroups: comp.lang.c Subject: Re: What if I don't free() all my dynamically allocated memory? Summary: Free it for portability Keywords: Not on a Unix system, that is. Message-ID: <2239@athertn.Atherton.COM> Date: 4 May 89 19:11:36 GMT References: <2580@ssc-vax.UUCP> <386@nbires.nbi.com> <1384@dukeac.UUCP> Organization: Atherton Technology, Sunnyvale, CA Lines: 38 In article <1384@dukeac.UUCP>, bet@dukeac.UUCP (Bennett Todd) writes: > In article <2580@ssc-vax.UUCP| dmg@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@Atherton.COM | sys{op,adm,prg,engr} says {decwrl,sun,hplabs!hpda}!athertn!paul | "oops..." ?