_stklen in Turbo C

Brian K. W. Hook jdb at palmetto.cis.ufl.edu
Thu Feb 7 00:35:33 AEST 1991


In article <1921 at ruunsa.fys.ruu.nl> boogaard at ruunsa.fys.ruu.nl (Martin vdBoogaard) writes:
>For a variety of reasons I'd like to be able to compile (on a DOS machine
>with Turbo C) with a stack that is larger than the usual 4kB. The manual
>(yes, I *have* read the FM) says I should simply set _stklen (defined in
>dos.h) to the desired value. I just can't find out where. If I put e.g.
>
>  _stklen = 10000;
>
>at the start of main (), nothing happens, i.e. when I deliberately cause
>a stack overflow by some silly recursion, the number of times my
>stack-consuming function can call itself before triggering the `Stack
>overflow!' run-time error is always the same. (I can even make _stklen 0
>if I like.) Actually, I don't understand how a program would be able to
>change its own stack size at run time. Is this really what happens (or,
>in my case, doesn't happen)? Who has a working example of how to do this?

Well, if you look at the code in the Turbo C++ Docs (which is all I have)
then you will notice that it states that you should define _stklen
as an extern.  _stklen is an internal variable that I BELIEVE is used
as part of the start-up C module....?

Anyway, here is how I do it (and it does work!)

#include /* Whatever needs to be include */

extern unsigned _stklen=8192;  /* This is off the top of my head, but I    */
                               /* believe _stklen is an unsigned int, but  */
                               /* it could be signed...                    */
void main ( void )   /* Of course you could change the args and returns */
{

/*
** Put your code here
*/

}

The key here is the extern declaration, that lets the compiler know that you
are referring to a variable that is in another module (the startup module
I believe)....If you put the declaration inside main() it wouldn't work,
especially if you didn't declare it an extern (if you didn't, TC would just
believe that you are declaring a new local variable).

Hope this helps,

Brian



More information about the Comp.lang.c mailing list