0
#include <stdio.h>

int main() {
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    printf("%p\n", &a);
    printf("%p\n", &b);
    printf("%p\n", &c);
    printf("%p\n", &d);

    return 0;
}

The results is: 0x7fff822a2678 0x7fff822a267c 0x7fff822a2680 0x7fff822a2684

2 Answers2

0

Does this answer your question Does stack grow upward or downward? ?

The behavior of stack (growing up or growing down) depends on the application binary interface (ABI) and how the call stack (aka activation record) is organized. Throughout its lifetime a program is bound to communicate with other programs like OS. ABI determines how a program can communicate with another program. The stack for different architectures can grow the either way, but for an architecture it will be consistent. Please check this wiki link. But, the stack's growth is decided by the ABI of that architecture.

clanglai
  • 149
  • 1
  • 6
0

The compiler is free to allocate local (auto) variables at any place on the local stack frame, you cannot reliably infer the stack growing direction purely from that. You can infer the stack grow direction from comparing the addresses of nested stack frames, ie comparing the address of a local variable inside the stack frame of a function compared to it's callee