During the execution of C + + program, the general direction of memory is divided into four areas
- Code area: it stores the binary code of the function body and is managed by the operating system
- Global area: stores global variables, static variables and constants
- Stack area: automatically allocated and released by the compiler to store function parameter values, local variables, etc
- Heap area: it is allocated and released by the programmer. If the programmer does not release it, it will be recycled by the operating system at the end of the program
1.1 before program operation
After the program is compiled, an exe executable program is generated. Before the program is executed, it is divided into two areas
Code area:
Store machine instructions executed by CPU
The code area is shared. The purpose of sharing is to have only one code in memory for frequently executed programs
The code area is read-only because it prevents the program from accidentally modifying its instructions
Global area:
Global and static variables are stored here
The global area also contains a constant area, where string constants and other constants are also stored
The data in this area is released by the operating system at the end of the program
//global variable int g_a = 10;int g_b = 10; //Global constant const int c_g_a = 10;const int c_g_b = 10; int main() { //local variable int a = 10; int b = 10; //Print address cout << "local variable a The address is: " << (int)&a << endl; cout << "local variable b The address is: " << (int)&b << endl; cout << "global variable g_a The address is: " << (int)&g_a << endl; cout << "global variable g_b The address is: " << (int)&g_b << endl; //Static variable static int s_a = 10; static int s_b = 10; cout << "Static variable s_a The address is: " << (int)&s_a << endl; cout << "Static variable s_b The address is: " << (int)&s_b << endl; cout << "The string constant address is: " << (int)&"hello world" << endl; cout << "The string constant address is: " << (int)&"hello world1" << endl; cout << "Global constant c_g_a The address is: " << (int)&c_g_a << endl; cout << "Global constant c_g_b The address is: " << (int)&c_g_b << endl; const int c_l_a = 10; const int c_l_b = 10; cout << "Local constant c_l_a The address is: " << (int)&c_l_a << endl; cout << "Local constant c_l_b The address is: " << (int)&c_l_b << endl; system("pause"); return 0; }
Summary:
- In C + +, the program is divided into global area and code area before running
- The code area is characterized by sharing and read-only
- Global variables, static variables and constants are stored in the global area
- The global constants and string constants decorated with const are stored in the constant area
1.2 after program operation
Stack area:
It is automatically allocated and released by the compiler to store the parameter values and local variables of the function
Note: do not return the address of local variables. The data opened up in the stack area is automatically released by the compiler
int * func() { int a = 10; return &a; } int main() { int *p = func(); cout << *p << endl; cout << *p << endl; system("pause"); return 0; }
Stacking area:
It is allocated and released by the programmer. If the programmer does not release it, it will be recycled by the operating system at the end of the program
In C + +, new is mainly used to open up memory in heap area
int* func() { int* a = new int(10); return a; } int main() { int *p = func(); cout << *p << endl; cout << *p << endl; system("pause"); return 0; }
Summary:
Heap data is managed and released by programmers
Heap data uses the new keyword to open up memory
1.3 new operator
Using new operator in C + + to open up data in heap
The data developed in the heap area is manually developed by the programmer, manually released, and released by using the operator delete
Syntax: new data type
For the data created with new, the pointer of the type corresponding to the data will be returned
Example 1: basic syntax
int* func() { int* a = new int(10); return a; } int main() { int *p = func(); cout << *p << endl; cout << *p << endl; //Using delete to release heap data delete p; //cout << *p << endl; // An error is reported. The free space is inaccessible system("pause"); return 0; }
Example 2: array
//Heap array int main() { int* arr = new int[10]; for (int i = 0; i < 10; i++) { arr[i] = i + 100; } for (int i = 0; i < 10; i++) { cout << arr[i] << endl; } //Release array delete followed by [] delete[] arr; system("pause"); return 0; }