Hey guys, I'm trying to count the amount of bytes of stack space that the following chunk of code will need. All local variables and temp results have their own location on the stack and their space is allocated at the beginning of the program and will not be reclaimed until the program exits:
I know this isn't a complete program, but I'm really curious how to measure the amount of bytes in stack space that this chunk of code will need. Any help is greatly appreciated.Code:{ int a; int b; int c; int x; a = 5; b = a + 3; c = a * b / a; x = c - (a + a) / (b - c); }
Each int I believe is 32 bytes so 32*4=128 bytes?
Thank you very much for the answer.
So just to clarify: the assignment statements do not require extra bytes on the stack? Nor the brackets? So in this chunk of code, only variable declarations require stack space?
Thanks again for the answer.
Also I'm assuming all local variables and temporary results will be given their own location on the stack and all locals and temporaries will have their space allocated at the beginning of the program and this space will not be reclaimed until the program exits.
Not sure if that changes anything...
It depends on how many bytes an int is on your compiler, how the memory is arranged, etc, etc, etc. There isn't a simple answer to your question other than "It's implementation defined".
WingedPanther has it exactly.
Different compilers do it differently. GCC for instance may try to optimize some variables by keeping a value in a CPU register instead of on the stack.
You can make rough guesses though but you'll never know unless you check out the assembly code (use the -S flag in GCC). In that code above, I would expect it to be removed at compile time unless you did something with one of the variables; if you did, then the values would likely be precomputed.
Clarification: An int is typically 32 bits NOT 32 bytes. That would be absolutely massive. You also have to keep in mind that variables are aligned for hardware compatibility, so 4-byte variables are aligned on addresses that are multiples of 4 bytes, 2-byte variables on 2-byte boundaries, etc. So if you have this:
the answer is eight--yes, eight--bytes. Only variables (and two other things you don't need to worry about at this point) take up stack space; the code is stored in the code section of the program, which is separate from the stack.Code:char a; int b;
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks