Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ukma!xanth!ames!ucsd!chem.ucsd.edu!tps From: tps@chem.ucsd.edu (Tom Stockfisch) Newsgroups: comp.lang.c Subject: Re: Memory Allocation Message-ID: <467@chem.ucsd.EDU> Date: 9 May 89 06:19:31 GMT References: <10946@bloom-beacon.MIT.EDU> <12546@ut-emx.UUCP> <29978@apple.Apple.COM> <17252@mimsy.UUCP> <1989May3.201118.10221@utzoo.uucp> <10202@smoke.BRL.MIL> <461@chem.ucsd.EDU> <10206@smoke.BRL.MIL> Reply-To: tps@chem.ucsd.edu (Tom Stockfisch) Organization: Chemistry Dept, UC San Diego Lines: 19 In article <10206@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >In article <461@chem.ucsd.EDU> I (Tom Stockfisch) write: >>You waste up to a factor of 4 with a power-of-2 implementation, >>not including fragmentation. >..., I see a factor of two (not four) here,... Here is the problem, at least with our BSD malloc(): I am reading in data that needs to be put in one contiguous array. I call malloc() with a certain amount, fill that up, call realloc(), fill that up, etc. Every time that I cross a power of two boundary, realloc() puts my old memory on the free list for the previous power of two and gets a whole new chunk. Thus, for a large final array size of n = (2^m) + 1 it has allocated 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + ... + 2^(m+1), which is nearly 2^(m+2), which is nearly 4*n. -- || Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu