Question about malloc() and free()

Don Lewis del at thrush.mlb.semi.harris.com
Fri Aug 31 10:53:55 AEST 1990


In article <2223 at idunno.Princeton.EDU> rssutor at broccoli.Princeton.EDU (Robert S. Sutor) writes:
>
>Another good argument to free what you have malloc'ed, as has been stated
>before (here?), is that someone might decode to use your program or functions
>as subroutines of another program.  In that case it is definitely not a good
>idea to leave dead data tying up memory.

#define asbestos_suit_on

I'm going to argue against doing this blindly.  There is one particular
application that I have used and which will remain nameless.  This
application reads a bunch of data files on startup and builds a large
data structure in memory.  The data structure is significantly larger
than than available physical memory, but since this program is running
on a machine with virtual memory (sound of page faults happening...),
here is no problem, right?  After it has finished reading the data, the
program walks around the data structure (sound of more page faults)
and writes its output (except the last stdio buffer full) to a file.
Then the program goes into serious page fault mode for about 45
minutes.  I don't know what it does during this time, but I'd be willing
to bet that it is walking the data structure, freeing all the bits and
pieces of memory it has malloc'ed along the way.  Finally it calls
exit() which fflush's the rest of the output data.

It sure would be considerate if this program just called exit()
instead of wasting my time and a lot of cpu cycles doing something
totally useless.  I'd even be happy with a strategically placed
fflush() so that I could send the process a well deserved "kill -9".

#define asbestos_suit_off

Now that feels much better.
--
Don "Truck" Lewis                      Harris Semiconductor
Internet:  del at mlb.semi.harris.com     PO Box 883   MS 62A-028
Phone:     (407) 729-5205              Melbourne, FL  32901



More information about the Comp.lang.c mailing list