www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/30/13:54:45

Message-Id: <199912301727.TAA10752@www.Foo.COM>
From: "S. M. Halloran" <mitch AT duzen DOT com DOT tr>
Organization: User RFC 822- and 1123-compliant
To: deleveld AT my-deja DOT com
Date: Thu, 30 Dec 1999 19:36:10 +0200
MIME-Version: 1.0
Subject: Re: question about stack frames
CC: djgpp AT delorie DOT com
In-reply-to: <84frs6$l3h$1@nnrp1.deja.com>
X-mailer: Pegasus Mail for Win32 (v3.12b)
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 30 Dec 99, deleveld AT my-deja DOT com was found to have commented thusly:

> I have a question about DJGPP and stack frames.
> 
> I am working on a recursive function that has to recursive
> quite a few times and I'd like to be able to calculate how many
> times the function can recurse without overflowing the stack.
> 
> My function is like this:
> 
> static void _draw_recursive(Window* const window,
>                             const ExposeEvent* const rect)
> {
>    ExposeEvent *event;
>    Window* ptr = window->higher;
> 
>    ... do some stuff with the arguments and local variables ...
> 
> }
> 
> Now I have already tried to reduce the amount and size of local
> variables. i.e. I need an Expose event and I malloc and free a
> pointer in the function instead of making it a local varaible.
> 
> What confuses me is when I look at the assembler output for
> the function I see this:
> 
> __draw_recursive:
> 	subl $44,%esp
> 	pushl %ebp
> 	pushl %edi
> 	pushl %esi
> 	pushl %ebx
> 	movl 64(%esp),%eax
> 
>    ... some other stuff ...
> 
> It looks to me like there are $44 bytes reserved on the stack
> when I am pretty sure that I need a lot less that that.  I
> calculate that I need at most 4 pointers i.e. 16 bytes.
> 
> Can anyone explain this do me?

You only show two pointers in your code fragement rather than 4.  That 
would suggest only 8 bytes, even less than you calculate.  Unless you are 
autoing variables within inner blocks.

At any rate, if the compiler wants 68 bytes for the locals, with four 
pushes of registers saved by convention, making at least 84 bytes of stack 
used in the call, plus another 4 bytes for the call itself, you could say 
the stack pointer is decremented at least 88 bytes with each call. 

Even with the default stack that DJGPP supplies (what is it? 512 K?), you 
can recurse nearly 6000 times before you are in trouble.  You anticipate 
doing it that heavily?

>  Any advice on making the stack
> frame as small as possible?

If you don't trust an optimizing compiler to do it, you have to do it 
yourself (i.e., write the assembly code).

Instead of recursion, what's the possibility of doing this with looping 
instead?  I think there are great many proponents for finding ways of 
doing it by looping rather than recursion.

> Doug Eleveld


Mitch Halloran
Research (Bio)chemist
Duzen Laboratories Group
Ankara       TURKEY

- Raw text -


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