[C++ learning] Dynamic memory management of C++ | The bottom layer of new and delete | Getting to know templates for the first time

Table of contents

1. Dynamic memory management in C++

2. The bottom layer of new and delete

3. Position new

4. Summary of the difference between new and malloc

5. Template

Write at the end:


1. Dynamic memory management in C++

The previous article has roughly introduced the usage of new and delete.

And the difference between the two dynamic memory management methods of C++ and C language, here is a brief summary:

1. Dynamically apply for data of built-in types:

There is no difference between new/malloc except for the usage above.

2. Dynamically apply for custom types of data:

In addition to the above usage of new/malloc, new/delete will also call the constructor initialization and destructor.

2. The bottom layer of new and delete

Let's see:

 

We can see that new and delete actually call these two global functions:

operator new and operator delete, and these two library functions can also be used directly

Look at the code:

 

#include <iostream>
using namespace std;

int main()
{
	int* p1 = new int;
	delete p1;

	int* p2 = (int*)operator new(sizeof(int));
	operator delete(p2);

	return 0;
}

We can see that from the usage point of view, the usage of these two functions is the same as malloc and free,

I am here to see:

The difference between malloc of new,

The function of new: open space + constructor

malloc function: open space

In fact, the underlying open space of new is directly used by malloc, but

Object-oriented language processing fails, do not like to use the return value, it is recommended to use throwing exception, and malloc fails to return empty,

So in fact, operator new is an encapsulation of malloc. If the application fails, operator new will throw an exception.

So new is to use operator new to open space + call the constructor.

3. Position new

The function of positioning new is to display the call constructor,

In normal times, the constructor is called automatically, and it is called automatically when the object is created.

The constructor is also called automatically when new is used,

Take a look at this code:

#include <iostream>
using namespace std;

class A {
public:
	A()
	{
		cout << "A()" << endl;
	}

	~A()
	{
		cout << "~A()" << endl;
	}
};

int main()
{
	A* p1 = new A;
	delete p1;

	//使用定位new模拟new的功能
	A* p2 = (A*)malloc(sizeof(A));
	new(p2)A; 

	p2->~A();
	free(p2);

	return 0;
}

output:

A()
~A()
A()
~A()

The usage of locating new is like this: new(p2)A; new(pointer) type.

Of course, if you need to pass parameters, you can also: new(p2)A(1), so that you can pass parameters to the constructor.

4. Summary of the difference between new and malloc

What malloc/free and new/delete have in common are:

They all apply for space from the heap and need to be released manually by the user.

The differences are:

1. malloc and free are functions , new and delete are operators

2. The space requested by malloc will not be initialized, but new can be initialized

3. When malloc applies for space, it is necessary to manually calculate the size of the space and transfer it.

New only needs to be followed by the type of space. If there are multiple objects, specify the number of objects in []

4. The return value of malloc is void*, which must be forced when used , and new does not need it, because new is followed by the type of space

5. When malloc fails to apply for space , it returns NULL.

Therefore, it must be judged as empty when used, new does not need it, but new needs to catch exceptions

6. When applying for a custom type object, malloc / free will only open up space, and will not call the constructor and destructor .

After applying for space, new will call the constructor to complete the initialization of the object.

delete will call the destructor to clean up the resources in the space before releasing the space.

Summarize:

The first four are the differences between features and usage,

The latter two are differences in the underlying implementation: return value/throwing exception, calling constructor and destructor

5. Template

Templates are a good thing,

Let me first look at such a piece of code:

#include <iostream>
using namespace std;

void Swap(int& x, int& y) {
	int tmp = x;
	x = y;
	y = tmp;
}

void Swap(double& x, double& y) {
	double tmp = x;
	x = y;
	y = tmp;
}

void Swap(char& x, char& y) {
	char tmp = x;
	x = y;
	y = tmp;
}

int main()
{

	return 0;
}

After we have learned function overloading, we can use this way,

Let us use exchange operations for these types, but it is too troublesome to write them all.

These are just three types, what if there are more types?

So what to do?

Look at the code:

#include <iostream>
using namespace std;

//也可以这样写:template<class T>
template<typename T>
void Swap(T& x, T& y) {
	T tmp = x;
	x = y;
	y = tmp;
}

int main()
{
	int a1 = 10, b1 = 20;
	double a2 = 1.1, b2 = 2.2;
	char a3 = 'a', b3 = 'b';

	Swap(a1, b1);
	Swap(a2, b2);
	Swap(a3, b3);

	return 0;
}

After using the template, all types of exchanges can be supported,

The T of the template is the template parameter, which defines the type.

At this time, there is another question. Do these three Swaps call the same function?

Let's see what assembly says:

 Found no, the addresses of the three functions are different, they call three different functions,

Because this Swap is just a template, it will generate the corresponding function according to your call,

And this process is called the instantiation of the template.

Like this picture:

So in fact we call these specific functions,

 In the end it is the compiler that takes care of it all and does the work we need to do.

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131509601