[C++] Memory model

Table of contents

1. Memory partition

2. Partition order

1 Before running the program

2 After running the program

3.new operator


1. Memory partition

The meaning of memory partitioning: Data stored in different areas are given different life cycles, giving us greater flexibility in programming

Memory can be divided into the following areas:

  1. Code area: stores the binary code of the function body and is managed by the operating system

  2. Global area: stores global variables, static variables and constants

  3. Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc.

  4. Heap area: allocated and released by the programmer. If the programmer does not release it, it will be reclaimed by the operating system when the program ends.

2. Partition order

1 Before running the program

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 code

The code area is shared . The purpose of sharing is that for frequently executed programs (double-click the exe multiple times), only one copy of the code is required in the memory.

The code area is read-only . The reason for making it read-only is to prevent the program from accidentally modifying its instructions.

Global area:

Global variables and static variables are stored here.

The global area also contains the constant area, where string constants and other constants are also stored.

The data in this area is released by the operating system after the program ends.

//全局变量
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地址为: " << (int)&a << endl;
	cout << "局部变量b地址为: " << (int)&b << endl;

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

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

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

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

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

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

	system("pause");

	return 0;
}

Summarize:

  • In C++, before the program is run, it is divided into a global area and a code area.

  • The code area is characterized by sharing and read-only

  • Global variables, static variables, and constants are stored in the global area.

  • The constant area stores const-modified global constants and string constants.

2 After running the program

Stack area:

Automatically allocated and released by the compiler to store function parameter values, local variables, etc.

Note: Do not return the address of local variables. The data opened 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;
}

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 when the program ends.

In C++, new is mainly used to open up memory in the 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;
}

Summarize:

The heap area data is managed by the programmer to open and release

Heap area data uses the new keyword to open up memory

3.new operator

Use the ==new== operator in C++ to open data in the heap area

The data developed in the heap area is manually developed by the programmer and released manually. The release uses the operator ==delete==

grammar:new 数据类型

Data created using new will return a pointer of the type corresponding to the data.

Example 1: Basic syntax

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; //报错,释放的空间不可访问

	system("pause");

	return 0;
}

Example 2: Open an 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;
	}
	//释放数组 delete 后加 []
	delete[] arr;

	system("pause");

	return 0;
}

おすすめ

転載: blog.csdn.net/qq_35902025/article/details/129846269