The memory occupied by a program compiled by C/C++ is divided into the following parts
1. Stack area (stack) - automatically allocated and released by the compiler, storing the parameter names of the function, the name of the local variables, etc. It operates similarly to the stack in a data structure.
2. Heap area (heap) - is allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list.
3. Global area (static area) (static area)—The storage of global variables and static variables is placed together. The initialized global variables and static variables are in one area, and the uninitialized global variables and the uninitialized static variables are in another adjacent area. After the program is finished, it is released by the system.
4. Text constant area - Constant string is placed here and is released by the system after the program is finished.
5. Program code area—Storing binary code for function body.
int a = 0;//Global initialization area
char*p1;//Global uninitialized area
main()
{
intb;//Stack
chars[] = "abc";//Stack
char*p2;//Stack
char*p3 = "123456";//123456\0 is in the constant area, p3 is on the stack.
static int c =0;//Global (static) initialization area
p1 = (char*)malloc(10);
p2 = (char*) malloc(20);//The allocated 10 and 20 bytes are in the heap area.
}
strcpy(p1, "123456"); 123456\0 is placed in the constant area, and the compiler may optimize it into one place with the "123456" pointed to by p3.