I. prerequisites-ProgramMemory Allocation
AC/C ++The memory occupied by compiled programs is divided into the following parts:
1Stack area (Stack)- Released automatically by the compiler Stores the function parameter values and local variable values. The operation method is similar to the stack in the data structure.
2, Heap area (Heap) - Usually assigned and released by programmers, If the programmer does not release the programOSReclaim . Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3, Global zone (static Zone )(Static)-The storage of global variables and static variables is stored in one area, and the initialized global variables and static variables are stored in one area, Uninitialized global variables and uninitialized static variables are in another adjacent area.-System release after the program ends
4, Text Constant Area -Constant strings are placed here. The program is released by the System
5, ProgramCodeZone-stores the binary code of the function body.
Ii. Example Program
This is written by a senior. It is very detailed.
// Main. cpp
Int A = 0;Global Initialization Zone
Char * P1;Global uninitialized Zone
Main ()
{
Int B ;//Stack
Char s [] = "ABC ";//Stack
Char * P2 ;//Stack
Char * P3 = "123456"; 123456 \ 0 ";//In the constant area,P3Stack.
Static int C = 0;//Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
//Allocated10And20The Byte area is in the heap area.
Strcpy (P1, "123456"); // 123456 \ 0In the constant area, the compiler mayP3Point"123456"Optimized to one place.
}