The memory occupied by a C/C ++ compiled program is divided into the following areas:
1. Stack)
It is automatically assigned and released by the system to store function parameter values and local variable values. It is a continuous storage area in the memory, extending from a low address to a high address.
2. Heap)
Distribution and release by programmers. If the programmer does not release the program, it may be recycled by the operating system at the end of the program. Its storage space is discontinuous in the memory, and the allocation method is similar to the linked list.
3. Static Zone)
It is also called the Global zone. After the program ends, it is released by the system to store global and static variables. The initialized global variables and static variables are in one area. uninitialized global variables and uninitialized static variables are stored in another adjacent area.
4. Text Constant Area
After the program ends, it is released by the system to store constants. string constants are placed here.
5. Code Area
Stores the binary code of the function body.
# Include <stdlib. h> # include <string. h> int A = 0; // global initialization zone. char * P1; // not initialized globally. int main (void) {int B; // stack area. char s [] = "ABC"; // stack zone. char * P2; // stack zone. char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack. static int C = 0; // static (global) initialization zone. // The allocated 10 and 20 bytes are in the heap area. P1 = (char *) malloc (10); P2 = (char *) malloc (20); // 123456 \ 0 is placed in the constant area. // The Compiler may optimize it to the "123456" pointed to by P3. strcpy (P1, "123456"); Return 0 ;}