Here is a very close-up of the mouth need to look carefully:
Destory (): shows that a destructor that invokes an object is equivalent to releasing some dynamic memory that an object needs to release to prepare for the next really freed object
DEALLOCATE (): Truly frees a block of memory. This is a top-level package that is the delete function of the upper package in the STL's Space configurator may call the delete back to the OS may also be placed in the memory pool but for the user is to free the memory block.
Look at an example should understand the real meaning:
1#include <iostream>2 3#include <stdlib.h>4 5#include <unistd.h>6 7 using namespacestd;8 9 classATen One { A - Public: - the A () - - { - +Data=New Char[1000000]; - + } A at~A () - - { - - //Free the data memory - in Delete[]data; - to } + - Private: the * Char*data; $ Panax Notoginseng }; - the intMainintargcChar*argv[]) + A { the + for(;;) - $ { $ -A *a=NewA; - theStd::allocator<a>Allocat; - WuyiAllocat.deallocate (A,sizeof(A)); the - Wu - } About $ return 0; - -}
View CodeObviously this is going to go wrong, and running to a certain time will throw an exception. So deallocate just frees the object memory and does not call the object's constructor to release the dynamic memory within the object this is important to note if a allocat.destory (a) is called; There's no bug in the program.
So in C + +, object release and dynamic memory release within an object is a memory leak that needs attention.
Here the delete operation symbol itself has 2 actions: Call destructor 2 to free memory. Instead:: operator delete only frees memory. The DEALLOCATE function is encapsulated based on the operator delete function. This needs to be remembered
Can be seen by the STL source code discovery: The destructor like Vector () has 2 operations: Destory () releases the dynamic memory within the object (if any) deallocate is freeing the object itself to occupy the memory through these 2 points will ensure that the memory can not be compromised
Correlation and differentiation of destory () and deallocate () and delete functions in stl--c++, destorydeallocate