On Thu, 19 Jun 2003, Muli Ben-Yehuda wrote:

> On Thu, Jun 19, 2003 at 02:54:53PM +0200, Eli Billauer wrote:
> > Hello Orna.
> > 
> > What I would do, is to write a small function which returns the value of 
> > the stack pointer, when it was called. It's not very accurate, but it 
> > can give a good indication of how heavily the stack is consumed.
> 
> There are two ways to do this, both straight out of "introduction to
> operating systems" 
> 
> int foo(void)
> {
>       char b; 
>       
>       unsigned long stackaddr = &b; 
> 
>       fprintf(stdout, "stack is roughly at %lx\n", stackaddr); 
> }
> 
> and the other, much more elegant, is using inline asm to get the value
> of %esp. 
> 
> #include <stdio.h> 
> 
> static inline unsigned long getstack(void)
> {
>       unsigned long stackaddr; 
> 
>       /* I think the constraints are correct, but won't bet on it */
>       asm volatile("movl %%esp,%0\n\t" :"=m" (stackaddr):); 
>       
>       return stackaddr;
> }
> 
> 
> int main(int argc, char* argv[])
> {
>       fprintf(stdout, "stack pointer: 0x%lx\n", getstack()); 
> 
>       return 0; 
> }
> 
> 

Thanks Muli, this is just what I looked for. Since the available stack 
size depends on the things allocated on the stack already, these functions 
let me know at a certain point in the calling hyrarchy, how much place is 
left on the stack.

I find it rather amusing that I need to write to Haifux to get an answer 
from you ;)

Nadav, I prefer your usual educated answers rather than your "compare to a
constant" function and condenscending attitude. I know that having
too-large a structure on the stack is bad, and that those should be 
allocated on the heap. What I needed to know was just how large 
is "too large". 

-- 
Orna.   |  http://vipe.technion.ac.il/~ladypine/

I am not a number, I am a free person!




--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://www.haifux.org)
To unsub send an empty message to [EMAIL PROTECTED]


Reply via email to