Comments: Authenticated sender is From: "George Foot" To: DougEleveld Date: Fri, 28 Aug 1998 19:03:25 +0000 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: question to malloc and free Reply-to: george DOT foot AT merton DOT oxford DOT ac DOT uk CC: djgpp AT delorie DOT com Message-Id: Precedence: bulk On 28 Aug 98 at 18:03, DougEleveld wrote: > Endlisnis wrote: > > > I've read that the size of the block allocated by malloc/new is always > > a power of 2, and rounded up to one if it isn't (after adding it's few > > words). Why does it do this? This seems (to me) like it would waste about > > 1/2 the RAM allocated. > > This way the memory actually requested is of a known size and the > allocations can be done _very_ fast. It's the clasic size/speed > tradeoff. But remember (I think) that only addressing space from the > 4Gig is wasted, not actual memory. (!Simplified explanation!) Because > if you allocated 1.2k and get 2k, and you never access out of the range > that you are allowed (which you should never do anyway) then the other > 0.8k will never get paged in, and it will make very little difference if > 0.8k out of the 4gig is wasted, especially since maximum real+virtual > memory is way less than 4gig anyway. I think in fact the paging is done with 4k granularity, so a whole 4k page is either in or out of physical memory at one time. So if you allocated 16k and only used less than 8k, the first two 4k pages would be paged in and the remaining 8k would stay paged out. IIRC the old malloc rounded up to 4k boundaries, so there was significant wastage here if you allocated a lot of very small structures (because the whole 4k page is paged in, and you only touch a bit of it). I might be misremembering though, I don't have the sources to that malloc. -- george DOT foot AT merton DOT oxford DOT ac DOT uk