[C++] Memory management

1. C/C++ memory distribution

Insert picture description here

1. The stack area grows from high address to low address, storing non-static local variables, function parameters, etc.
2. The heap area grows from low address to high address, storing malloc, new variables.
3. Data segment stores global variables and static variables
. 4. Code segment stores read-only constant light, executable code.

2. Introduced from C language memory management

C language dynamic memory management: link .
This is the blogger's summary of the dynamic development of memory management in C language, including basic usage and common errors

3. C++ memory management

In C++, our system defines two keywords for memory management.

Basic operations for built-in types

Note: To apply for and release the space of a single element, use the new and delete operators, to apply for and release continuous space, use new[] and delete[]
Insert picture description here

Managed objects

	//申请空间
	int* ptr = new int;
	
	//申请空间并初始化
	int* ptr2 = new int(1);
	delete ptr;
    delete ptr2;
	

Manage an array of objects

    //申请大小为4*10的连续空间
	int* arr = new int[10];
	//c++98不允许连续空间初始化
	//c++11后支持
	//	int *arr =new[10](1);(是错误代码)  
    delete[] arr;

Basic operations for custom types

Compared with built-in types, the new and delete functions of custom types are more abundant:
new will call the constructor to initialize (new is only an application for space)
delete will call the destructor to clean up the space

#include <iostream>
using namespace std;
class Test
{
    
    
public:
	Test()
		: _data(0)
	{
    
    
		cout << "Test():" << this << endl;
	}
	~Test()
	{
    
    
		cout << "~Test():" << this << endl;
	}
private:
	int _data;
};
void Test2()
{
    
    
	// 申请单个Test类型的空间
	Test* p1 = (Test*)malloc(sizeof(Test));
	free(p1);
	// 申请10个Test类型的空间
	Test* p2 = (Test*)malloc(sizeof(Test) * 10);
	free(p2);
}
int main()
{
    
    
	Test* t = new Test;
	delete t;
	return 0;
}

Insert picture description hereIt can be seen that their constructors and destructors are called, and it is a physical space.
Note:
1. There must be a default structure to open up dynamic space.

operator new和operator delete

1. New and delete are operators for users to apply and release dynamic memory. Operator new and operator delete are global functions provided by the system. New calls the operator new global function at the bottom to apply for space, and delete uses the operator delete global function at the bottom. Free up space.
2. Operator new actually applies for space through malloc. If malloc
successfully applies for space, it will return directly. Otherwise, the user-provided space shortage measures will be executed. If the user provides such measures, the application will continue, otherwise an exception will be thrown. Operator delete finally releases space through free.

It can be understood that operator new encapsulates malloc and exception handling mechanism.
Insert picture description here

New&delete realization principle

void test1(){
    
    
//内置类型
	int* ptr = new int;
	//new->operator new->malloc
	delete ptr;
	//delete->operator delete->free
	int* ptr1 = new int[10];
	//new[]-> operator new[]-> operator new->malloc
	delete[] ptr1;
	//delete[]->operator delete[]->operator delete->free

//自定义类型
	Test* t1 = new Test;
	//new->operator new->malloc->构造
	delete t1;
	//delete->析构->operator delete->free
	Test* t2 = new Test[10];
	//new[]->operator new[]->operator new->malloc->10构造
	delete t2;
	//delete[]->10析构->operator delete[]->operator delete->free


}

The difference between malloc/free and new/delete

  1. malloc and free are functions, new and delete are operators
  2. The space requested by malloc will not be initialized, new can be initialized
  3. When malloc applies for space, you need to manually calculate the size of the space and pass it. New only needs to be followed by the type of space.
  4. The return value of malloc is void*, which must be forced when used. New is not required, because new is followed by the type of space
  5. When malloc fails to apply for space, it returns NULL, so it must be null when used, new does not need, but new needs to catch exceptions
  6. When applying for a custom type object, new will call the constructor to complete the initialization of the object after applying for space, and delete will call the destructor to complete the cleanup of resources in the space before releasing the space.

Guess you like

Origin blog.csdn.net/zhaocx111222333/article/details/114982845