malloc(), virtual memory

John P. Linderman jpl at allegra.UUCP
Wed Sep 11 01:04:53 AEST 1985


> The size you hand to malloc has (in our C library at any rate) 4
> added to it, then the result is brought up to the nearest power of
> two.  So if you give it ((1 << 22) - 4) you will get a 4M chunk;
> one more byte and malloc will require an 8M chunk.
> -- 
> In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)

Chris is right (as usual) about the 4.2 malloc being about 50%
efficient when allocating blocks whose size is a power of 2,
(like the buffers that stdio will allocate).  Another ``gotcha''
to beware of is that space, once allocated, is never broken
into smaller pieces.  For example, if I allocate a 4 meg
temporary workspace, free it, then allocate a 2 meg area,
malloc will not reuse the freed space, it will try for a new
area, and, thanks to the aforementioned quirks, it will fail
with the standard 6 meg per-process limit.  Dunno if this is
fixed under 4.3.  One can hope, but I wouldn't bet the farm.

John P. Linderman  Space Cadet  allegra!jpl



More information about the Comp.lang.c mailing list