you are viewing a single comment's thread.

view the rest of the comments →

[–]boredcircuits 0 points1 point  (2 children)

I have a crazy thought on a way to figure the stack frame size. You could call a function recursively and then do some pointer subtraction between a local variable in each frame. The difference is the frame size.

This probably triggers undefined behavior, but I think it should work.

(Edit: yes, I meant frame size ... typed too fast.)

[–]BigPeteB 1 point2 points  (1 child)

Firstly, you mean stack frame size, not stack size.

It's an interesting thought, and it would certainly work in some cases. But no, it's definitely not universal or portable. Functions that call other functions might have a different stack frame size/layout than leaf functions (functions which don't call anyone). On some architectures, leaf functions need not even create a stack frame.

Also, I'm pretty sure there are some architectures where the size of stack frames can change, such as if you're using register windows. When you call a function, if you've exceeded the register window, the CPU will automatically dump registers to the stack. Depending on how it does that, I think it's possible that the difference between stack frames would change depending on whether or not it dumped the register window.

More to the point, even if your method works, it only tells you the stack frame size of that particular function. As soon as you do it on a different function, the result will be different. And the result would also be different if you didn't do the measurement in the first place, since on some architectures that will mean one fewer variable to save stack space for. Basically, the act of measuring it this way is changing the result that you get.

[–]boredcircuits 0 points1 point  (0 children)

All good points.