Turbo C stack size
sbs at csc.fi
sbs at csc.fi
Sun Jul 22 04:35:02 AEST 1990
In article <1990Jul20.150505.511 at ceres.physics.uiowa.edu>, mic at sun.soe.clarkson.edu (Mic Lacey) writes:
> Message-ID: <1990Jul20.154753.23714 at sun.soe.clarkson.edu>
> Organization: Clarkson University, Potsdam, NY
> Date: 20 Jul 90 15:47:53 GMT
> Lines: 41
> PQ
> Andy,
> I have had the same problem with running out of stack space
> using Turbo C 2.0. I am pretty sure (but by no means positive) that
> you are limited to 64k worth of stack space. This becomes a problem
> when you call functions that allocated large local variables (as these
> variables are allocated on the stack when the function is called)d
> It can also be a problem if you call may functions from within functions
> (this is a common problem with recusive functions). I suspect you are
> running out of space because your functions allocate too many variables
> on the stack. A possibile solution to this problem is to malloc those
> large variables when the function begins (and make sure to free them
> before leaving the function). When you malloc a variable the space is
> allocated off the heap, which is the space above the stack and below
> the top of memory. Try something like this:
>
> int foo(int c)
>
> {
>
> char *array;
>
>
> array = (char *) malloc(SOME HUGE NUMBER);
>
> ...
> }
>
> instead of:
>
> int foo(int c)
>
> {
> char array[SOME HUGE NUMBER];
>
> ...
>
> }
>
> Good luck!!
Hi!
I'm just developing a program on 286 which uses a lot of DOS-memory under
640K limit. The workaround you have presented above is not a good solution
because an individual array (structure, union,...) has to be less than
64Kbytes unless you use so called huge -pointers and 'farmalloc' in TURBO C.
So an improvement will be:
> int foo(int c)
>
> {
>
> char huge *array;
>
>
> array = (char huge *) farmalloc((unsigned long)SOME HUGE NUMBER);
>
> ...
> }
In addition to this I use some helpful 'defines' to maximize portability
(I normally transfer program to VAX/VMS or UNIX later on):
(1) Set into .h-file for example:
#ifdef MSDOS
#ifndef POINTER
#define POINTER huge
#endif
#ifdef POINTER==far || POINTER==huge
#define malloc farmalloc /* casting would give better results */
#endif
#else
#define POINTER
#endif
My foo-function becomes now:
> int foo(int c)
>
> {
>
> char POINTER *array;
>
>
> array = (char POINTER *) malloc((unsigned long)SOME HUGE NUMBER);
>
> ...
> }
And I compile the foo.c:
> tcc -ml -DMSDOS -DPOINTER=huge foo.c
Note that it is now simple matter to use 'normal' near or far pointer by
giving for example "-DPOINTER=far".
regards,
Sami Saarinen, Centre for Scientific Computing, Finland
(sbs at csc.fi)
More information about the Comp.lang.c
mailing list