definition
Destructors, also known as destructors, are called automatically when objects are destroyed. They are generally used to clean up objects.
There is no return value, no parameter, and cannot be overloaded. There is only one destructor, unlike constructors, which can be overloaded.
In the following program, ~ Person() function is the destructor of the Person class. It has the same name as the class, but it is preceded by a "~"
class Person { public: Person();//This is the constructor. I've learned it ~Person();//This is the destructor, preceded by a '~' private: }; //Once the destructor is called, the object is considered destroyed Person::~Person() { cout << "Person::~Person()" << endl; }
The main function of destructors
Destructors are mainly used to clean up memory, and the most important thing is to clean up memory in heap space. Because if the object is stored in the stack space, it will be retracted after the call is completed. The heap space needs to be released manually only when the pointer object is created to point to the heap space, so most destructors are used to release the memory of the heap space.
class Person { public: Person();//This is the constructor. I've learned it ~Person();//This is the destructor, preceded by a '~' Person *p_new1; private: }; //Once the destructor is called, the object is considered destroyed Person::~Person() { delete p_new1; cout << "Person::~Person()" << endl; } void test() { Person person; person.p_new1 = new Person;//In this new space in the user-defined function, after the function is executed, the pointer * p_new1 will be reclaimed by stack space, but the heap space pointed to by the pointer will not be reclaimed, so it must be released in the destructor } int main() { test(); getchar(); return 0; }
Just like P in the above program_ New1 pointer. Although the pointer itself is created in stack space, it points to heap space. After the test function is called, the pointer object P of Person_ New1 will be recycled by stack space, but the heap space pointed to is not recycled, which will cause memory leakage. So in P_ After the new1 object is destroyed, you need to manually reclaim the heap space pointed to, so you can leave this to the destructor. Because the destructor is called automatically after the object is destroyed!
Objects allocated through malloc will not call the destructor when free is released
We know that the object allocated by malloc will not automatically call the constructor, and the destructor will not be called when free releases the object.
class Person { public: ~Person();//This is the destructor, preceded by a '~' Person *p_malloc private: }; //Destructor Person::~Person() { delete p_malloc; cout << "Person::~Person()" << endl; } int main() { Person *p_malloc = (Person*)malloc(sizeof(Person)); free(p_malloc);//The destructor will not be called, so heap space memory will not be freed getchar(); return 0; }
For objects allocated through new, the destructor will be called when delete is released
class Person { public: ~Person();//This is the destructor, preceded by a '~' Person *p_new private: }; //Destructor Person::~Person() { delete p_new; cout << "Person::~Person()" << endl; } void test() { Person person; person.p_new = new Person;//In this new space in the user-defined function, after the function is executed, the pointer * p_new1 will be reclaimed by stack space, but the heap space pointed to by the pointer will not be reclaimed, so it must be released in the destructor } int main() { Person *p_new = new Person;//After creating the object, * p_new is recycled, but what about the object memory in the heap space??? free(p_new);//new in the main function cannot be released in this kind of destructor, because the stack space will recover the pointer * P when the main function ends_ new, and the heap space pointed by the pointer must be released manually, //In other words, * P_ The new pointer will not be recycled at all, because after the main function ends, the program ends, but if the code is in another function, it's OK test(); getchar(); return 0; }
If you know that the main function ends, it means that the program ends. Therefore, if the object is created in the main function, there is no need for a destructor, because the object will not be released until the main function is completed. However, when developing projects, we mostly create objects in ordinary functions, such as the test() function. After the test function is called, the object will be recycled by the stack space. At this time, the heap space has not been recycled, so we need the destructor to release the heap space.
Difference between malloc and new
malloc creates an object without calling the constructor, that is, it cannot initialize members through the constructor; When free releases an object, the destructor is not called.
The constructor is called when new creates an object, and the destructor is called when detele.
So don't use malloc if you can use new!!!
summary
- Objects are created in stack space, and destructors are called automatically when objects are recycled
- Create a pointer in the stack space to point to the memory of the heap space. After the stack space reclaims the pointer, since the heap space has not been released, it is necessary to release the heap space in the destructor at this time. This is also the greatest use of destructors!
- When an object is created in the global area, the destructor will not be called, because its life cycle is the whole program running cycle!