?
Solved

Function Stack Size

Posted on 2003-03-30
9
Medium Priority
?
530 Views
Last Modified: 2006-11-17
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
Comment
Question by:beneflex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Expert Comment

by:GaryFx
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

by:Hermetic
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

by:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Assisted Solution

by:grg99
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

by:Kocil
ID: 8239992
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.
 
0
 
LVL 2

Expert Comment

by:honey_hamster
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

by:grg99
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

by:jmcg
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.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question