malloc and free problems

michael sweet sweetmr at SCT60A.SUNYCT.EDU
Mon Apr 22 21:18:28 AEST 1991


> Hi, I wish to allocate 10 times as much memory as I really need to initialize
> ...
> using the huge amount of memory I started with, even after supposedly
> disposing of it.  What is going on here?

Well, the *big* problem is that most implementations of malloc() use the sbrk()
function to get more memory.  Unfortunately, there is no 'unsbrk()' function
to return the memory to the system.  On some systems, memory is allocated onto
the end of an existing block, making selective freeing of memory impossible.

The only way around this is to roll you own malloc() function (see K&R 2nd
edition) which keeps track of the highest-used address for a block of memory.
When that block is completely free, you can call sbrk() with a negative number
(the size of the block negated) to return the memory to the system.  Note
however that even this will not garentee that the memory is returned to the
system, tho you can be confident that large blocks *will* be...

 -Mike Sweet

------------------------------------------------------------------------------
"The only        TASC                      (315) 724-1100 (voice)
 truth is that   555 French Road           (315) 724-2031 (fax)
 there are no    New Hartford, NY  13413   Internet: sweetmr at sct60a.sunyct.edu
 truths, only beliefs."                    Delphi:   DODGECOLT
------------------------------------------------------------------------------



More information about the Comp.sys.sgi mailing list