From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: failed malloc/callocs :( Date: Sun, 04 May 1997 20:57:09 +0000 Organization: Two pounds of chaos and a pinch of salt Lines: 44 Message-ID: <336CF825.5F47@cs.com> References: <5ki242$qe8 AT bore DOT pipex DOT net> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp110.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk nikki wrote: > > this works fine on a 32 meg machine under win95 or dos (with cwsdpmi) > on a 16 meg machine it gives the insufficient memory error, despite having > plenty of memory ( 120 meg virtual, 14 meg real). > > there are no other (large) memory allocations in the program, this is the only > one. so why does it fail? and how come needmem() said it would be fine? > > does anyone have any suggestions as to how to improve needmem() so it always > works, and also how to make this run on a 16meg machine? The answer is simple - don't use _go32_dpmi_remaining_*_memory(). They aren't reliable enough to use for such a task, because they only reflect the state of the operating system and DPMI host, not the internal state of the program itself. The DJGPP memory allocation routines recycle allocated memory in order to save time. This means that once your program grabs a chunk of memory, it's allocated forever as far as the DPMI host is concerned. If you free() it, it just goes back onto the list of free blocks in malloc()'s internal structures, and is not actually freed to the system. But for the task you are attempting, you shouldn't need to use such a clumsy workaround. malloc() and calloc() both return NULL pointers if they fail to allocate the desired amount of memory. A simple test will suffice: if ( ( ptr = malloc( 1024 * 1024 ) ) == NULL ) { fprintf( stderr, "malloc failed\n" ); exit( 1 ); } All the problems of handling the internal structures, grabbing memory from the DPMI host, adjusting the free memory values, are all done for you. There's no need to reinvent the wheel. -- --------------------------------------------------------------------- | John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com | | * Proud user of DJGPP! * | http://www.cs.com/fighteer | | ObJoke: If Bill Gates were a robber, not only would he | | shoot you, but he'd send you a bill for the bullets. | ---------------------------------------------------------------------