Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Function Stack Size

Posted on 2003-03-30
Medium Priority
540 Views
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 :=)
0
Question by:beneflex

LVL 6

Expert Comment

ID: 8236541
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
0

LVL 1

Expert Comment

ID: 8236693
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?
0

LVL 5

Accepted Solution

Kocil earned 200 total points
ID: 8237828
#include <stdio.h>

int stack(int a_first, int a1, int a_last)
{
int l_first=1;
int l1,l2;
int l_last=2;

l1=l2=l_first = l_last = a_first; /* tricking the optimizer */
printf("Stack-size = %d\n", (char*)&a_last - (char*)&l_last);

}

main()
{
stack(1,2,3);
}
0

LVL 22

Assisted Solution

grg99 earned 200 total points
ID: 8238050
It's hard to calculate this.  -Kocil- comes close, by doing some arithmetic on the address of the first parameter pushed, minus the address of a local variable.   To this you might have to add some space for any variables declared inside any blocks in the function.
And of course if this function calls anything, either explicitly by you, or calls generated by the compiler, that's stack space too.

One way I've used to figure out exactly how much stack space a function uses: call a function that sets a big swatych of stack space below you to some known value, then at function exit, scan this space and see how much of it has been clobbered. Something like this:

#define MaxZap 100000

void AtFuncEntry()
{  int i; char Zap[ MaxZap ]; for(i=0; i<MaxZap ; ) *i++ = '#'; }

int AtFuncExit()
{  int i; char Zap[ MaxZap ]; for(i=MaxZap ; *i-- == '#'; ) ; return( MaxZap - i ); }
0

LVL 5

Expert Comment

ID: 8239992
Mine is only for 1 function as beneflex asked.
The grg's is for whole program.

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.

0

LVL 2

Expert Comment

ID: 8240078
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.
0

LVL 22

Expert Comment

ID: 8240083
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!
0

LVL 20

Expert Comment

ID: 10199296
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.

jmcg
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month10 days, 12 hours left to enroll