Link to home
Start Free TrialLog in
Avatar of beneflex
beneflex

asked on

Function Stack Size

How do I calculate the stack size of this function?

void foo(int i)
{
   int i = 0;
   printf("%d\n", i);
}

Besides the local variable, do I need to consider function header?

Thanks :=)
Avatar of GaryFx
GaryFx

Do you mean the space used on the stack by a call to this function?

If so, then there's the function call block, which at a minimum includes space for the return address, and may include space for the return value, processor flags, and other data.  This structure varies among different platforms.

This doesn't include the stack space consumed by the printf, which would be quite difficult to calculate without the source code.

Gary
Do you need to calculate the maximum size the stack reaches during runtime or ???  If I may ask, what do you need this information for?
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mine is only for 1 function as beneflex asked.
The grg's is for whole program.

I like your trick grg.
Just make sure MaxZap is slightly less than the stack size, or you'll get stack overflow error or
segmentation fault.

When I was working with Intel C compiler for embedded system, there was a utility (profiler) that can determine maximum stack size. Therefore we can set the efficient stack size for the ROM-ed program. I don't know if VC, or BC have this, because in PC this issue is not as critical as for embedded system.
 
You've got a parameter 'i' and local variable 'i' inside your subroutine foo(); I'm sure it's just a typo.  And for a more useful comment - different compilers do different things with the stack.  For your example above, some compilers may put the parameter 'i' into a CPU register, and may also store the local variable in a different CPU register.  By adding the code suggested above that takes the address of the local variable, that may force the compiler to actually allocate space on the stack for it instead of using one of the CPU registers.  One sure way of determining the amount of stack space required by a subroutine call is to look at the assembler code generated by the compiler.

BTW, I've seen printf() require 100s of bytes of stack when used in a DOS 'C' program.
Oh, and I forgot, if you're running on a simple OS, any hardware interrupts or system API calls may end up using a little bit of your stack space.

This used to be a a HUGE problem on the original Macintosh-- some of the fancier graphics region API calls would use up ALL the space on the stack, all the way down to the top of the heap.  But you're probably not programming for that!
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between Kocil and grg99.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer