The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. STACK: automatically allocated by the compiler when the program is running, storing the function parameter values and local variable values. The operation method is similar to the stack in the data structure. 2. Heap-another storage area is opened in the memory. Generally, it is assigned to the programmer for release. If the programmer does not release the program, 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. The allocation method is similar to the linked list. 3. Global (static)-memory allocated upon compilation by the compiler. Global variables and static variables are stored in one area, and initialized global variables and static variables are stored in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The program is released by the system after it ends. 4. The text Constant Area-constant string is placed here. The program is released by the system. 5. program code area-stores the binary code of the function body.
Note: static local variables and static global variablesThe amount of static storage is not necessarily a static variable. For example, a global variable is a static storage method, but not necessarily a static variable. It must be defined by the static variable before it can become a static external variable or a static global variable. After a local variable is changed to a static variable, its storage mode is changed, that is, its lifetime is changed.
After changing a global variable to a static variable, it changes its scope and limits its scope of use.
See the following example:
# Include <iostream> using namespace STD; char * func () {// char STR [] = "Hello, world \ n"; // error string array, local variable, stored in the stack; char * STR = "Hello, world \ n" can be modified; // The okay String constant is stored in the constant zone; return STR cannot be modified ;} int main () {cout <func (); Return 0 ;}