Re: Required stack size

From: Keith Thompson (kst-u_at_mib.org)
Date: 02/28/05


Date: Mon, 28 Feb 2005 21:57:55 GMT

Kasper Dupont <kasperd@daimi.au.dk> writes:
> mkovac@mail386.com wrote:
>>
>> Hi all,
>>
>> I would like to know how much stack memory is used when program is
>> running. Can this be achived in some portable way? At least I would
>> like to know this on Linux. I already managed to get this with the help
>> of gcc instrument functions to check the value of stack pointer when
>> program is entering in functions.
>
> This is the most portable I can come up with:
>
> char *approx_stack_pointer()
> {
> char dummy;
> return &dummy;
> }
>
> Just call this from your main function and when the
> stack is highest, and substract the results. Take
> the numerical value of the result (as you don't know
> if the stack is growing up or down).

(I think you mean absolute value rather than numerical value.)

You should be aware that the C language doesn't guarantee that this is
going to work. Comparing or subtracting pointers that don't point
into the same object (or just past the end of it) invokes undefined
behavior.

Having said that, it's likely to work on many systems; just don't
expect it to be *completely* portable.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


Relevant Pages

  • Re: StdCall vs. CDecl
    ... I don't understand why "leaving arguments on the stack" is benign. ... If the caller expects the callee to "clean up the stack" (that is, restore the stack pointer to the value it had before the caller pushed a call frame onto the stack), then I would expect the caller to be thoroughly messed up after the call returns and the caller tries to get to its own data on the stack. ... That's for calling a cdecl function as stdcall. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: [9fans] pointer to the last TOS
    ... the stack pointer is there. ... a positive offset from ... pointer to the virtual frame pointer to find the return address. ...
    (comp.os.plan9)
  • Re: TCB for pthread in linux kernel 2.4
    ... more detail exactly what you mean by garbage collection? ... > it should know other threads' current stack pointer to form a root-set ... > let the kernel module read relevant threads' stack-pointer and pass ...
    (comp.os.linux.development.system)
  • Re: Reviewing history
    ... While a stack architecture has significant ... performance would seem likely to use a more flexible register set. ... possibly allowing the stack pointer to ... and the contents would be saved in front of a stack frame on ...
    (comp.arch)
  • Re: Developing/compiling software
    ... This makes IAR more difficult to setup and tune, and GCC slower and clunkier because it has to disable interrupts for a few instructions on every function call. ... GCC is very popular on the AVR though, and is good enough for most applications, especially used in combination with the other free AVR tools such as AVRStudio. ... I have not gone back to check, but from memory the AVR uses two 8 bit registers to implement a 16 bit stack pointer. ...
    (comp.arch.embedded)