MBOX-Line: From owner-djgpp-list AT delorie DOT com Wed Dec 4 10:45:55 1996 Date: Wed, 04 Dec 1996 12:55:41 +0000 From: Bill Currie Subject: Re: Minor problem with operator new[] Cc: djgpp AT delorie DOT com Reply-to: billc AT blackmagic DOT tait DOT co DOT nz Message-id: <32A574CD.68F8@blackmagic.tait.co.nz> Organization: Tait Electronics NZ MIME-version: 1.0 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7bit References: <5800sc$5jp AT ns2 DOT southeast DOT net> Status: O To: alex AT adam DOT com DOT au Precedence: bulk M. Edward Kiser wrote: > > /* > Recently, just out of curiosity, I decided to see how much memory I > could allocate under various conditions. I wrote this program to find > out, based on the assumption that operator new[] would throw an > exception if it couldn't allocate the memory. But it doesn't throw the > exception. It prints a message > > "Virtual memory exceeded in new" > > and terminates the program. This is very un-ANSI-like! > > I searched the DJGPP FAQ (v2.01) and the djgpp\info\gcc.* files and > couldn't find any hints on how to change this "bad" behavior. So... > here I am. > > > /* > Typical output (Windows 95, with 20 MB of physical RAM): > Trying to allocate 2 bytes... succeeded. > Trying to allocate 4 bytes... succeeded. > Trying to allocate 16777216 bytes... succeeded. > Trying to allocate 33554432 bytes... Virtual memory exceeded in `new' > */ Double all the above values, and that is the amount of memory you ar realy allocating (with a minimum of 8 bytes, but an 8 byte chunk goes to 16 bytes) This is due to the way malloc (which is called by new) works. malloc adds 4 (usually, but can be 8 if malloc.c is compiled for debugging) to the requested size and then rounds the result up to the next power of 2. This behaviour is an optimization for speed rather than memory use. Workaroun: define your own `operator new' for each object and manage pools of memory yourself. Bill -- Leave others their otherness.