C++ dynamic memory management (new /delete-new[]/delete[]-N releases)

 

Before learning the management of C++ dynamic memory, let's review the dynamic memory management of C language. This is a blog about dynamic memory management of C language written by me: https://blog.csdn.net/porryCn/article/details/ 80280708 .

A brief review:

 

 

C language uses malloc/calloc/realloc/free for dynamic memory management.

 

 
int main() {
	int* a = (int*)malloc(sizeof(int)*4);//malloc 开辟4个大小为4*sizeof(int)内存空间;

	int* b = (int*)calloc(4, sizeof(int));//calloc 相比malloc,它的功能是开辟内存空间后的同时初始化为0;
	int* c = (int*)realloc(b,sizeof(int)*10);//realloc 可以在指定地址空间或者非指定地址空间开辟指定大小的内存空间。
	//   c指针要开辟的空间显然比指定的指定空间要大,就要考虑到原地址空间的容量是否满足再开辟空间的大小,如果不够就要重新寻找合适的内存空间进行开辟。
	//分配成功后,原地址空间自动释放;
	//具体实例可以查看我的关于C语言动态内存管理相关博客。
	
	free(a);  //使用后要记得释放
	free(c);	//同上
   //
	return 0;
}

 

 

 

C++ uses new / delete for dynamic memory management ()

:new (applying space in the heap in C++)

, delete (release space)

{

    new/delete=dynamic management object;

    new[]/delete[]=dynamic management object array;

}

The difference from malloc/free is that malloc/free is a standard library function, while new/delete is an operator. The former is to apply for free memory, while the latter is to apply for memory and initialize and release objects.

First make a simple chestnut to understand:

 

#include<iostream>
using namespace std;
int  main() {
	int* p1 = new int;//动态分配一个4字节(int)空间
	int* p2 = new int(10);//动态分配一个4字节(一个int)的空间,并初始化为10。
	int* p3 = new int[10];//动态分配40个字节(10个int)的空间。

	delete p1;
	delete p2;
	delete[] p3;

	return 0;
}

 

When we use dynamic memory management in C/C++, we must remember a principle, use matching! Use to match! Use to match! , otherwise it will cause memory leaks or even program crashes! ! !

Matching usage principles: malloc (calloc/realloc) and free and new/new[] and delete/delete[];

#include<iostream>
using namespace std;
//全局变量
int a = 1;
//全局静态变量
static int b = 2;
int  main() {
	//局部变量/静态变量
	int a1 = 1;
	static int b1 = 2;

	char arr1[] = "studing make me happy";
	char* arr2 = "yes";

	int* a = (int*)malloc(sizeof(int)*2);
	int* b = (int*)calloc(4,sizeof(int));
	int* c = (int*)realloc(a,sizeof(int)*4);
	free(a);
	free(c);

	int* p1 = new int;
	int* p2 = new int(10);
	int* p3 = new int[10];
	delete p1;
	delete p2;
	delete[] p3;
	return 0;
}

 

 

 

 

 

1) The stack is also called the stack, non-static local variables/function parameters/return values, etc. The stack grows downward.

2) The memory mapping segment is an efficient I/O mapping method for loading a piece of shared memory. Users can create shared memory through the interface for inter-process communication.

3) The heap, which is used for program running is dynamic memory allocation, and the heap grows upward.

4) Data segment --- store global data and static data.

5) Code segment --- executable code/read-only constant.

 

 

 

----------------------------------------------------------------------------------------------------------------------

We all know that C++ is compatible with C. Since there is already a dynamic memory allocation method, why add new/delete?

 

 

1. Although both are entry points for dynamic memory allocation, malloc/free are C/C++ standard library functions, and new/delete are operators.

2. malloc needs to calculate the size of the type by itself and the return value is void*, new automatically calculates the size of the type, and the return value is the pointer of the corresponding type.

3. malloc/free is only the development and release of dynamic memory, while new/delete is to call the constructor and destructor to open up dynamic memory and initialize and clean up the space.

 

 

------------------------------------------------------------------------------------------------------------------------

Other C++ dynamic memory management interfaces: operator new / operator delete and operator new[]/ operator delete[]

 

 

A brief introduction (compared to malloc/free and new/delete):

1. The usage is the same as malloc/new, and it is also used in matching, which is actually the encapsulation of malloc/new.

2. operator new / operator new[] allocates space, operator delete / operator delete [] frees space, they do not call constructor and destructor to initialize objects and clean up objects.

 

--------------------------------------------------------------------------------------------------------------------

The relationship between new / delete and operator new / operator delete and malloc / free

 

 

 

 

 

-----------------------------------------------------------------------------------------------------------------------------

The mechanism of the underlying processing when new[]

 

 

 

After reading the blog of a big guy, I was quite touched. I cut the summary of the big guy :

For built-in types: 


new does not define the length of the array in the first 4 bytes of the first address. 
delete and delete[] have the same execution effect, they will delete the entire array, and the length to be deleted can be known from new. 
For custom types:

 
new [] defines the length of the array 4 bytes before the first address. 
When delete[], the destructor is executed to delete the entire array according to the length defined by the first 4 bytes. 
If you just delete the first address of the array, only the value of the first object will be deleted. 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Summarize:

malloc/free is used to manage dynamic memory in C language, while C++ uses new and delete to dynamically manage objects, and new[]/delete[] is used to dynamically manage dynamic object arrays.
When using it, we must match it.
Types: malloc/free are library functions, new/delete are operators in C++.
In fact, the bottom layer of new/delete is implemented by malloc/free. malloc/free is only responsible for opening and releasing memory, while new and delete are not only responsible for opening and releasing memory, but also for the construction and destructor functions. When new[]/delete[] is used, the constructor and destructor will be called N times.
When opening up memory, malloc requires type conversion, and the size of the memory developed by the technology is not needed by new. It automatically calculates the type size and returns the corresponding type.
If the development fails. malloc returns NULL, while new throws an exception. After failure, a new_handler function will be called, which will try to open up memory, provided that you need to set the new_handler function yourself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324154199&siteId=291194637