Xref: utzoo comp.lang.c:40558 comp.os.msdos.programmer:6001 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!unisoft!hoptoad!wet!smiller From: smiller@wet.UUCP (Gregory Shane Miller) Newsgroups: comp.lang.c,comp.os.msdos.programmer Subject: Re: Does TC's farrealloc have a bug? Summary: min memory malloc and stack interferes! Keywords: farrealloc,realloc,memory Message-ID: <2643@wet.UUCP> Date: 28 Jun 91 04:39:33 GMT References: <1991Jun19.083945.8921@ucthpx.uct.ac.za> <1991Jun20.074613.1279@donau.et.tudelft.nl> Followup-To: poster Organization: Wetware Diversions, San Francisco Lines: 31 I once wrote a program which did around 580 to 600K of farmallocs and too found out that I ran out memory long before I anticipated. I believe this is the answer to your question. First, when any of TC's malloc functions are called, it merely passes the malloc call to the DOS malloc functions. You will notice that the DOS malloc functions allocates INTEGER UNITS of PARAGRAPHS (16 bytes). So when you do malloc(6) DOS does malloc(16) or when you do malloc(12) it does malloc (32). Also, my program was very recursive and when in deep levels the stack gets so big it reduces the far heap space. That is the problem your running into. I should also point out that the function farcoreleft() is useless after the first malloc. It only reports the space between the highest allocated byte and the bottom of stack. When the heap becomes fragmented, this function returns useless numbers. The only thing it is good for is too find the maximum amount of heap available - remember this goes down when the stack pushes down into memory. In order to solve my problem, I calculated the entire amount of memory I needed and did one big malloc and then rewrote my recursive routine to be iterative (using as it turned out the harddisk to shuffle some data). Hope that helps -- BTW if you want the info from farcoreleft() use it as the first line of your main(), for even calls to scanf have an embedded malloc call that you don't see! smiller@wet.UUCP -- G. Shane Miller [ smiller@wet.UUCP (415) 453-4926 ]