[C++] Dynamic memory management ④ (Extended thoughts on dynamic creation and release of objects | Basic data type memory analysis | malloc allocates memory delete releases | new allocates memory free releases memory)





1. Extended thoughts on dynamic creation and release of objects



malloc and free are functions in the C language stdlib standard library, used to allocate and reclaim heap memory;

new and delete are operators in the C++ language, used to allocate and reclaim heap memory;

In the C++ language, it is compatible with the malloc and free usage of the C language, but it is recommended to use new and delete for dynamic memory management;


In general:

  • Memory allocated using malloc needs to be released using free;
  • Memory allocated using new needs to be released using delete;

So if the memory applied using malloc can be released using delete?

Can the memory applied for using new be released using free?


The following is a discussion of several situations in which different types of data apply for memory:

  • Allocate memory for underlying data types
  • Allocate memory for array data type data
  • Allocate memory for class objects




2. Memory analysis of basic data types




1. malloc allocates memory delete releases memory


Memory allocated for basic types using the malloc function can be released using delete;


In the code below,

Use the malloc function to apply for a memory space of type int in the heap memory.

int* p = (int*)malloc(sizeof(int));

Then use delete to release the memory space. The program executes normally without reporting an error. This means that for the heap memory space applied for the basic type using the malloc function, the delete operator can be used to complete the functions of the free function and successfully release the memory space;

// malloc 申请的内存 使用 delete 释放
delete(p);

Code example:

#include "iostream"
using namespace std;

int main()
{
    
    

	// C 语言中动态申请内存
	int* p = (int*)malloc(sizeof(int));
	*p = 10;

	cout << "*p = " << *p << endl;

	// malloc 申请的内存 使用 delete 释放
	delete(p);
	

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Results of the :

*p = 10
Press any key to continue . . .

Insert image description here


2. new allocates memory and free releases memory.


Memory allocated for basic types using the new operator can be released using free;


In the code below,

Use the malloc function to apply for a memory space of type int in the heap memory.

int* p = (int*)malloc(sizeof(int));

Then use delete to release the memory space. The program executes normally without reporting an error. This means that for the heap memory space applied for the basic type using the malloc function, the delete operator can be used to complete the functions of the free function and successfully release the memory space;

// malloc 申请的内存 使用 delete 释放
delete(p);

Code example:

#include "iostream"
using namespace std;

int main()
{
    
    

	// 使用 new 为基础类型在堆内存中申请内存空间
	int* p = new int;
	*p = 10;

	cout << "*p = " << *p << endl;

	// new 申请的内存 使用 free 释放
	free(p);
	

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Results of the :

*p = 10
Press any key to continue . . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/133122773