Correlation and differentiation of destory () and deallocate () and delete functions in stl--c++, destorydeallocate

Source: Internet
Author: User

  • 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 Code

    Obviously 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.