[C++ core] C++ memory partition model analysis

When the C++ program is executed, the general direction of memory is divided into 4 areas

  • Code area : storage 函数体的二进制代码, managed by the operating system
  • Global area : stores sums 全局变量and 静态变量constants
  • Stack area : automatically allocated and released by the compiler,存放函数的参数值,局部变量等
  • Heap area : 由程序员分配和释放, if the programmer does not release it, it will be reclaimed by the operating system at the end of the program

The meaning of the four areas of memory: the data stored in different areas are endowed with 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

​Store代码区: the machine instructions executed by the CPU,

1. The code area is shared .The purpose of sharing is that for programs that are frequently executed, only one copy of the code needs to be in memory
2. The code area is read-only .The reason for making it read-only is to prevent a program from accidentally modifying its instructions

​Global全局区: variables and static variables are stored here.

The global area also contains the constant area, where string constants and other constants are stored.The data in this area is released by the operating system after the program ends.

Example:

#include<iostream>
using namespace std;

//全局变量
int g_a = 10;
int g_b = 10;

//全局常量
const int c_g_a = 10;
const int c_g_b = 10;

int main() {
    
    

	//局部变量
	int a = 10;
	int b = 10;

	//打印地址
	cout << "局部变量a地址为: " << &a << endl;
	cout << "局部变量b地址为: " << &b << endl;

	cout << "全局变量g_a地址为: " <<  &g_a << endl;
	cout << "全局变量g_b地址为: " <<  &g_b << endl;

	//静态变量
	static int s_a = 10;
	static int s_b = 10;

	cout << "静态变量s_a地址为: " << &s_a << endl;
	cout << "静态变量s_b地址为: " << &s_b << endl;

	cout << "字符串常量地址为: " << &"hello world" << endl;
	cout << "字符串常量地址为: " << &"hello world1" << endl;

	cout << "全局常量c_g_a地址为: " << &c_g_a << endl;
	cout << "全局常量c_g_b地址为: " << &c_g_b << endl;

	const int c_l_a = 10;
	const int c_l_b = 10;
	cout << "局部常量c_l_a地址为: " << &c_l_a << endl;
	cout << "局部常量c_l_b地址为: " << &c_l_b << endl;

	return 0;
}

The execution result is:

局部变量a地址为: 0x309764408
局部变量b地址为: 0x309764404
全局变量g_a地址为: 0x1029720c0
全局变量g_b地址为: 0x1029720c4
静态变量s_a地址为: 0x1029720c8
静态变量s_b地址为: 0x1029720cc
字符串常量地址为: 0x10296deb7
字符串常量地址为: 0x10296dec3
全局常量c_g_a地址为: 0x10296df4c
全局常量c_g_b地址为: 0x10296df50
局部常量c_l_a地址为: 0x309764400
局部常量c_l_b地址为: 0x3097643fc

Summarize:

  • C++ is divided into global area and code area before the program runs
  • The code area is characterized by shared and read-only
  • Store global variables, static variables, and constants in the global area
  • Store const-modified global constants and string constants in the constant area

2. After the program runs

栈区:​Automatically allocated and released by

Note: Do not return the address of the local variable, the data created in the stack area will be automatically released by the compiler

Example:

#include<iostream>
using namespace std;

int * func()
{
    
    
	int a = 10;
	return &a;
}

int main() {
    
    

	int *p = func();

	cout << *p << endl;
	cout << *p << endl;

	return 0;
}

堆区:​It is allocated and released byIn C++, new is mainly used to open up memory in the heap area

Example:

#include<iostream>
using namespace std;

int* func()
{
    
    
	int* a = new int(10);
	return a;
}

int main() {
    
    
	int *p = func();

	cout << *p << endl;
	cout << *p << endl;
	
	return 0;
}

Summarize:

  • The heap area data is managed and released by the programmer
  • The heap area data uses the new keyword to open up memory

3. The new operator

Use in C++newThe operator develops data in the heap area, and the data developed in the heap area is manually developed by the programmer and released manually, and the release uses the operatordelete

​ Grammar: new 数据类型

​ The data created by new will return a pointer of the type corresponding to the data

Case 1: Basic Grammar

#include<iostream>
using namespace std;

int* func()
{
    
    
	int* a = new int(10);
	return a;
}

int main() {
    
    
	int *p = func();

	cout << *p << endl;
	cout << *p << endl;

	//利用delete释放堆区数据
	delete p;

	//cout << *p << endl; //报错,释放的空间不可访问

	return 0;
}

Case 2: Open up an array

#include<iostream>
using namespace std;

//堆区开辟数组
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;
	}
	//释放数组 delete 后加 []
	delete[] arr;

	return 0;
}

Guess you like

Origin blog.csdn.net/cui_yonghua/article/details/131343153