[comp.lang.c] Turbo C stack size

mic@sun.soe.clarkson.edu (Mic Lacey) (07/20/90)

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!!

mic@sun.soe.clarkson.edu (Mic Lacey) (07/21/90)

Message-ID: <1990Jul20.154753.23714@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!!

sbs@csc.fi (07/22/90)

In article <1990Jul20.150505.511@ceres.physics.uiowa.edu>, mic@sun.soe.clarkson.edu (Mic Lacey) writes:
> Message-ID: <1990Jul20.154753.23714@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@csc.fi)