Mail Archives: djgpp/1997/06/12/06:33:26
Kay Hayen wrote:
>
> I'd need to have a growing stack for at least one recursive function that
> does some sorting stuff.
>
> Is there a way to achieve this?
The stack is set to a fixed size when your program runs; the maximum
size can be changed either by setting the global variable _stklen in
your source code, or by using 'stubedit' on your program after it is
compiled. The default stack size is 256K.
Theoretically, it might be possible to dynamically increase the size of
the stack at runtime, but it would take a lot of work to do. However,
because the stack, like all other allocated memory, is only paged in
when it is actually used, you could set it to as high a value as you
want and be confident that it would not eat memory unless your recursive
function actually used that much space.
IMHO, it's a bad idea to write code that depends so heavily on the stack
that it needs more than a couple of megabytes. The first thing to do is
minimize the use of automatic variables and function arguments. Store
as much data as possible in static or dynamic structures. This may also
have the side effect of speeding up your code. Oberhumer Markus
(k3040e4 AT c210 DOT edvz DOT uni-linz DOT ac DOT at) posted a function on the
djgpp-workers mailing list that can detect the space remaining in the
stack when it is called; the code is below. Please note that I have not
tested it personally; it may not work on all computers, in all times and
places and phases of the moon. ;)
--snip--
/* make return value signed, just in case we already overflowed the
stack */
extern __inline__ int _stackavail(void)
{
extern unsigned __djgpp_stack_limit;
unsigned sp;
__asm__ __volatile__ ("movl %%esp,%k0\n" : "=r" (sp) : );
return sp - __djgpp_stack_limit;
}
--snip--
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| Proud owner of what might one | http://www.cs.com/fighteer |
| day be a spectacular MUD... | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------
- Raw text -