delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/12/06:33:26

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Growing stack with djgpp
Date: Wed, 11 Jun 1997 23:52:07 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 48
Message-ID: <339F3A27.3304@cs.com>
References: <6Yg$C-lxr5B AT jocokko DOT edition DOT bonbit DOT org>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp214.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019