Are C stack variables stored in reverse?

后端 未结 3 1936
无人共我
无人共我 2020-12-07 02:47

I\'m trying to understand how C allocates memory on stack. I always thought variables on stack could be depicted like structs member variables, they occupy successive, conti

相关标签:
3条回答
  • 2020-12-07 02:56

    Almost all the processors architectures nowadays supports stack manipulation instruction (e.g LDM,STM instructions in ARM). Compilers with the help of those implements stack. In most of the cases when data is pushed into stack, stack pointer decrements (Growing Downwards) and Increments when data popped from stack.

    So it depends on processor architecture and compiler how stack is implemented.

    0 讨论(0)
  • 2020-12-07 02:58

    Stack organization is completely unspecified and is implementation specific. In practice, it depends a lot of the compiler (even of its version) and of optimization flags.

    Some variables don't even sit on the stack (e.g. because they are just kept inside some registers, or because the compiler optimized them -e.g. by inlining, constant folding, etc..).

    BTW, you could have some hypothetical C implementation which does not use any stack (even if I cannot name such implementation).

    To understand more about stacks:

    • Read the wikipage on call stacks, tail calls, threads, and on continuations

    • Become familiar with your computer's architecture & instruction set (e.g. x86) & ABI, then ...

    • ask your compiler to show the assembler code and/or some intermediate compiler representations. If using GCC, compile some simple code with gcc -S -fverbose-asm (to get assembler code foo.s when compiling foo.c) and try several optimization levels (at least -O0, -O1, -O2 ....). Try also the -fdump-tree-all option (it dumps hundred of files showing some internal representations of the compiler for your source code). Notice that GCC also provides return address builtins

    • Read Appel's old paper on garbage collection can be faster than stack allocation, and understand garbage collection techniques (since they often need to inspect and possibly change some pointers inside call stack frames). To know more about GC, read the GC handbook.

    Sadly, I know no low-level language (like C, D, Rust, C++, Go, ...) where the call stack is accessible at the language level. This is why coding a garbage collector for C is difficult (since GC-s need to scan the call stack pointers)... But see Boehm's conservative GC for a very practical and pragmatic solution.

    0 讨论(0)
  • 2020-12-07 03:19

    Depends on the compiler and platform. The same thing can be done in more than one way as long it is done consistently by a program (this case the compiler translation to assembly, i.e. machine code) and the platform supports it (good compilers try to optimize assembly to get the “most” of each platform).

    A very good source to deeply understand what goes behind the scenes of c, what happens when compiling a program and why they happen, is the free book Reverse Engineering for Beginners (Understanding Assembly Language) by Dennis Yurichev, the latest version can be found at his site.

    0 讨论(0)
提交回复
热议问题