Article Directory
One, the memory partition model
When the C++ program is executed, the memory is generally divided into4 areas
-
Code area: store the binary code of the function body, managed by the operating system
-
Global area: store global variables and static variables and constants
-
Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc.
-
Heap area: allocated and released by the programmer, if the programmer does not release, the operating system will reclaim it at the end of the program
==The meaning of the four memory areas:==Data stored in different areas gives us different life cycles, giving us greater flexibility in programming
1. Before the program runs
After the program is compiled, an exe executable program is generated, which is divided into two areas before the program is executed
Code area:
-
Stores the machine instructions executed by the CPU
-
The code area issharedYes, the purpose of sharing is that for frequently executed programs, you only need to have a copy of the code in memory
-
The code area isRead onlyYes, the reason for making it read-only is to prevent the program from accidentally modifying its instructions
Global area:
-
Global variablewithStatic variableStore here
-
The global area also containsConstant area, String constants and other constants are also stored here
-
Features: The data in this area is released by the operating system after the program ends
Global variables and local variables are distributed in different memory areas in the memory
Global variables are variables that are not placed in functions
Static variable is to add static before ordinary variable
Global variables, static variables, string constants, global constants (const-modified global constants) are stored in the global area
Local variables and local constants (const-modified local variables) are not in the global area
Example
#include<iostream>
using namespace std;
//全局变量
int g_a = 10;
int g_b = 20;
//全局常量
const int c_g_a = 10;
const int c_g_b = 20;
int main()
{
//局部变量
int a = 10;
int b = 20;
cout << "局部变量a的存放地址" << (int)&a << endl;
cout << "局部变量b的存放地址" << (int)&b << endl;
//局部常量
const int c_a = 10;
const int c_b = 20;
cout << "局部常量c_a的存放地址" << (int)&c_a << endl;
cout << "局部变量c_b的存放地址" << (int)&c_b << endl;
//全局变量
cout << "全局变量g_a的存放地址" << (int)&g_a << endl;
cout << "全局变量g_b的存放地址" << (int)&g_b << endl;
//全局常量
cout << "全局常量c_g_a的存放地址" << (int)&c_g_a << endl;
cout << "全局常量c_g_b的存放地址" << (int)&c_g_b << endl;
//静态变量
static int s_a = 10;
static int s_b = 20;
cout << "静态变量s_a的存放地址" << (int)&s_a << endl;
cout << "静态变量s_b的存放地址" << (int)&s_b << endl;
//字符串常量
cout << "字符串常量的存放地址" << (int) &"hello world" << endl;
system("pause");
return 0;
}
Output result
2. After the program runs
Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc.
(Note: Do not return the address of the local variable, the data opened in the stack area will be automatically released by the compiler)
Local variables, formal parameter dataStored in the stack area, the data in the stack area is automatically released after the function is executed
Heap area: allocated and released by the programmer, if the programmer does not release it, the operating system will reclaim it when the program ends
New operator:
In C++, new is mainly used to open up memory in the
heap area. The data opened up in the heap area is created by the programmer, manually released, and released. Use the operator delete
syntax: new 数据类型
the data created by new will return the type corresponding to the data Pointer
#include<iostream>
using namespace std;
int *func()
{
int * p = new int(10);
return p;
}
void test01()
{
int * p = func();
cout << *p << endl;
cout << *p << endl;
//堆区的数据不会自动释放,需要程序员操作
//如果想释放堆区的数据,利用关键字delete
delete p;
}
void test02()
{
//在堆区创建整型数据的数组,含有10个元素
int *arr = new int[10];
//释放数组
delete[] arr;
}
int main()
{
test01();
system("pause");
return 0;
}
[Note] The learning course is-Dark Horse Program C++ Tutorial