Memory arrangement of variables in C language
There was some confusion about the arrangement of C variables in memory, so the following aspects were briefly sorted out (all examples are compiled based on GCC on X86 PC Ubuntu).
The increasing direction of memory addresses allocated to different variables when defining variables
(1)Define two global variables, print their allocated addresses in memory after assignment compilation, and find that the variables defined first are allocated in the lower addresses in memory.
(2)X86 PC Ubuntu main function defines variables, use gcc compilation to check the memory address allocated by the system to variables (memory allocation growth direction is low address to high address)
The direction in which memory addresses allocated to members of a structure grow after instantiation of the structure
(1)Define the global structure and initialize it. After gcc compilation, view the growth direction of memory allocation of structure members (the growth direction of memory allocation is from low to high)
(2)In the main function for the structure and initialization, gcc compiled to view the structure member memory allocation growth direction (memory allocation growth direction is from low to high upward growth)
The memory growth direction of the stack space used when defining variables in functions
arm When using arm cross-compiling tool chain, the memory allocation of general variables is upward growth direction, but for the memory allocation on the stack, the full stack reduction method is used to allocate memory on the stack to variables, and the growth direction of the stack is downward growth direction.