How to calculate the Stack Size?
Dear All,
I want calculate the used Stack Size..till now i did like this..my processor
assume my processor stack size is 8k bytes...
Code:
#include<stdio.h>
void main()
{
int m=0, n=0, sum=0;
sum=m+n;
printf("%d", sum);
return 0;
}
since i have three local variables stack consumed is 3 bytes so remaining stack size is=8k-3 bytes=1021 bytes
Am i right?
Thanks for useful information...
Quote:
Originally Posted by
matsp
The size of int is probably 4 (or perhaps 2), so three of those is 12 bytes (possibly 6).
However, in a traditional system, main is not the first thing on the stack, so there would be a few other things, and main itself will use some space on the stack - probably at least 2 integer sizes (8 or 4 bytes depending on size of integer).
And of course, calling printf will take one pointer (to the format string) and one integer space on the stack. Pointers are commonly 4 bytes, but may also be 2 or 8 bytes, depending on the type of processor (and perhaps also the settings in the system).
8KB is 8192 bytes, by the way. I've never heard of anyone counting stack size in bits, as most processors aren't capable of addressing 1 bit, but does it in bytes.
In summary, it is not entirely trivial to calculate the exact stack-size of a function. You could have a look at the assembler code - it will start by pusihing one or two registers onto the stack, then subtract something from the current stack pointer.
--
Mats
Whatever you explained is only for main function..if i have functions in main..i mean every function call take how many stack bytes....Example
Code:
#include<stdio.h>
int add(int, int);
void main()
{
int m=10,n=20; /* it will take 2 *4 =8 bytes
printf("%d", add(m,n)); / * printf will take 4 bytes+ function call will take 2 bytes to store add
}
int add(int x, int y)
{
return (x+y);
}
8k=8*1024=8192-(8+6)=8178 bytes remaining..Am i right?
Logic to develop the code for finding stack size?
Dear All,
I Have Complex code in my project..i have to calculate the algorithm for that...i am trying to develop C program for that..Any body know the logic how can implement this?