The memory occupied by a C/C++ compiled program is divided into the following parts:

(1) Stack area (stack) : automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. It operates like a stack in a data structure, with addresses extending from high to low.

(2) heap: generally allocated and released by the programmer, the memory allocated by the dynamic application during the execution of the program, the address extends from low to high, and the stack area is just the opposite. If the programmer does not release it, the program may be recycled by the OS at the end of the program. Note that this is not the same thing as a heap in a data structure and is allocated in a similar way to a linked list.

(3) Global area (static area static) : global variables and static variables are stored together, initialized global variables and static variables in one area, uninitialized global variables and uninitialized static variables in another adjacent area. The system is released after the program ends.

(4) Text constant area: Constant string is placed here. The program is released by the system after completion.

(5) Program code area: store binary code of function body.



The following code looks at the memory allocation:

int a = 0; // global initializer char* p1; Void main(void) {int b; // stack char s[] = "SSSSSS "; / / stack char * (p2); // stack char* p3 = "p3p3p3"; Char * p4 = "p4p4p4"; Static int c = 0; static int c = 0; // global (static) initializer p1 = (char*)malloc(10); // heap p2 = (char*)malloc(20); // heap __asm("sim"); SysClkInit(); delay_init( 16 ); Uart1_Init( 9600 ); __asm( "rim" ); while( 1 ) { delay_ms( 500 ); printf("a :%6d, %#x\r\n",a,&a); printf("p1:%6s, %#x\r\n",p1,&p1); printf("b :%6d, %#x\r\n",b,&b); printf("s :%s, %#x\r\n",s,&s); printf("p2:%6s, %#x\r\n",p2,&p2); printf("p3:%s, %#x\r\n",p3,&p3); printf("p4:%s, %#x\r\n",p4,&p4); printf("c :%6d, %#x\r\n",a,&c); printf("\r\n"); }}Copy the code

After running the program, the print result is as follows:



It can be seen from the running result that the addresses of global variables A, P1 and static variable C are next to each other, and the address of A is the smallest.

The addresses of other variables are s – > B – > P3 – > P4 – P2.

(1) Text segment Code area instructions are executed sequentially according to the programming flow. For sequential instructions, they are executed only once (per process). If they are repeated, they need to use jump instructions. Code segment: A code segment is usually an area of memory used to store the code that a program executes. The size of this area is determined before the program runs, and memory areas are usually read-only. Some architectures also allow code segments to be writable, that is, to modify the program. It is also possible to include read-only constant variables, such as string constants, in a code snippet. The instructions in the code area include the opcode and the object (or object address reference) to operate on. If it is an immediate number (that is, a concrete value, such as 5), it will be included directly in the code; If the data is local, space is allocated in the stack area and the data address is referenced. If it is a BSS section and a data section, the data address will also be referenced in the code. In addition, the code segment also plans the memory space information requested by the local data.

(2) Globally initialize Data Segment/static Data Segment. Initialize only once. Data segment: A data segment is usually an area of memory used to store initialized global variables in a program. The data segment is a static memory allocation. The static data area in the data section stores global variables, static variables and constants that have been initialized in the program.

(3) Uninitialized data area (BSS). Change its value at run time. BSS segment: A BSS segment is usually an area of memory used to store uninitialized global variables in a program. BSS is short for Block Started by Symbol. The BSS segment is a static memory allocation, that is, it is zeroed out at the beginning of the program. The BSS section will normally be cleared during initialization.

(4) Stack area. It is automatically allocated by the compiler to store function parameter values, local variable values, etc. Stores function parameter values, local variable values, and the context content of the current task when switching tasks. It operates like a stack in a data structure. Whenever a function is called, the function returns the address and some information about the call, such as the contents of certain registers, is stored in the stack area. The called function then allocates space on the stack for its automatic and temporary variables. This is how C implements recursive calls to functions. For each recursive function call, a new stack frame is used so that variables in the new instance stack are not confused with variables in the other instance stack of the function. Stack: a stack is used to store temporary local variables created by the program. That is, variables defined in the bracket “{}” are not included in the static declaration. Static means to store variables in a data segment. In addition, when a function is called, its parameters are pushed onto the stack of the process that initiated the call, and the return value of the function is pushed back onto the stack when the call ends. Because of the first-in, first-out nature of the stack, it is particularly convenient to save/resume the call scene. In this sense, we can think of the stack as an area of memory where temporary data is stored and exchanged.

(5) the heap. Used for dynamic memory allocation. The heap is located in memory between the BSS area and the stack area. Generally, it is allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS when the program ends. Heap: The heap is used to store memory segments that are dynamically allocated during the running of a process. Its size is not fixed and can be dynamically expanded or shrunk. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap expands); When memory is freed using functions such as free, the freed memory is stripped from the heap (the heap is reduced). When an application is loaded into memory space for execution, the operating system is responsible for loading code segments, data segments, and BSS segments, and will allocate space for these segments in memory. Stack segments are also allocated and managed by the operating system without the need for programmers to manage them explicitly. Heap segments are managed by the programmer himself, that is, by explicitly claiming and freeing space.