Does TC's farrealloc have a bug?

Gregory Shane Miller smiller at wet.UUCP
Fri Jun 28 14:39:33 AEST 1991


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 at wet.UUCP

-- 
G. Shane Miller [ smiller at wet.UUCP (415) 453-4926 ]



More information about the Comp.lang.c mailing list