2

Simple question: Is there a way to determine the stack size of a function?

int stackframe_size(int run) {
    int i ;
    if(!run) {
        return ((int)(&i) - stackframe_size(++run));

        }
        return (int)(&i);

    }

int main() {
    int x, y;
    double d;
    char c;

    int a = 4;
    int b = 5;
    int we = 6;
    int e = 123123;
    int hmm = 34453;
    int lol = 45;
    int asd = 23;
    x = 1;
    y = g(x);
    d = f(x, y, x-y);
    c = 'a';
    printf("%d", stackframe_size(0));
}

I am running the function I obtained from another thread to find the call stack size and it always seems to return 48...is there another way to find out or is this the only way?

Senescence
  • 63
  • 1
  • 7
  • why would you need to know it? – Jack Feb 05 '16 at 00:26
  • 3
    That's highly implementation dependent. – lost_in_the_source Feb 05 '16 at 00:26
  • Same function will have fixed stack frame size. Is there any problem? – Holsety Feb 05 '16 at 00:32
  • 3
    the code in `stackframe_size` does not refer to anything from `main`'s stack frame... you are returning the stack frame size of `stackframe_size` (or something akin to it) – M.M Feb 05 '16 at 00:52
  • 4
    Why is something starting with "simple question" that often **not** a simple question? Why does no one start with "complicated question"? So: simple answer: no simple way, except for a full assembler-path unit-test. – too honest for this site Feb 05 '16 at 00:59
  • 1
    *Highly* dependent on both your compiler and its settings, but not all of your local variables will always end up on the stack. The compiler may determine in one of its earliest passes that you don't modify them, and so they don't "need" to go onto the stack but may simply be implemented as constants. – Jongware Feb 05 '16 at 01:25
  • 1
    This is highly dependent also on whether the variables are actually used or not. Optimizers may take them out completely if they are assigned to but not modified. – owacoder Feb 05 '16 at 02:04
  • 1
    Possible duplicate of [Finding stack frame size](http://stackoverflow.com/questions/21501222/finding-stack-frame-size) – Roman Zaitsev Feb 05 '16 at 09:28

0 Answers0