Effective C + + clause 52 wrote placement new also to write placment delete

Source: Internet
Author: User

1. Placement new and place ment delete refer to the normal overloaded versions of operator new and operator Delete, the so-called normal operator new and delete, referring to versions with the following normal signature:

void operator New Throw (std::bad_alloc); void operator Delete (voidthrow();  // normal signature in global scope void operator Delete (normal signature in voidthrow//class scope
View Code

Because C + + allows additional parameters to be added to the overloaded operator new and operator delete, operator and operator delete with additional parameters are placement new and placement Delete. One of the more useful versions of the placement new version is "accept a pointer to the object where it is constructed", whose main purpose is to accept a pointer and then return it for the constructor to construct the object on, with the signature:

void operator New (std::size_t,voidthrow();

This version of new has been incorporated into the C + + standard library, which requires #include<new>, and usually refers to placement new, which refers to this version.

2. For the following statement:

widget* pw1=new Widget;  Widgets are a class

A total of two functions are called: one is operator new for allocating memory, and the other is the widget default constructor used to construct the widget.

Assuming that the first function call succeeds and the second throws an exception, then the memory requested by step 1 must be freed, or memory leaks. This task client cannot do this, so it is done by the C + + Runtime system: The runtime system invokes the corresponding operator called by step one Delete version. The so-called "corresponding" version of operator new with the normal signature is the operator delete (listed in 1) with the normal signature, and for placement new, "corresponding" refers to the same additional parameters.

Suppose the widget is defined as follows:

classwidget{ Public:    ...    Static void*operator New(std::size_t size,std::ostream& LogStream)Throw(Std::bad_alloc);//Placement operator New    Static void operator Delete(void* pmemory,std::size_t size)Throw();//normal operator Delete    ...};
View Code

If for the following statement:

widget* pw=New (Std::cerr) Widget;

If the statement throws an exception in the widget default constructor, it is the responsibility of the runtime system to cancel the assignment of operator new and restore had cleared, as stated above, and it needs to find

Static void operator New Throw (Std::bad_alloc);

The corresponding operator delete to perform the task, but no signature is:

Static void operator Delete Throw ();

operator delete. The result is "if a operator new with an extra parameter does not have a corresponding version operator delete with the same extra parameter, then there is no operator when new's allocation action needs to cancel and reply to had cleared." Delete is called, and memory leaks are unavoidable. The workaround is to declare and define a operator delete for the widget that corresponds to the previous operator new with the additional parameters:

classwidget{ Public:    ...    Static void*operator New(std::size_t size,std::ostream& LogStream)Throw(Std::bad_alloc);//Placement operator New    Static void operator Delete(void* pmemory,std::size_t size)Throw();//normal operator Delete    Static void operator Delete(std::size_t size,std::ostream& LogStream)Throw();//the corresponding operator delete    ...};
View Code

After this change, if the following statement throws the widget constructor throws an exception:

widget* pw=New(Std::cerr) Widget;

The corresponding placement delete is called.

It is important to note that only if an exception is thrown, the call will be the corresponding placement delete, if the above statement is performed properly, then execute:

Delete pw;

The normal version of operator delete is called.

3. In addition, the name masking rules for C + + can lead to unintended consequences, assuming that a base class is declared as follows:

class base{public    : ...     Static void operator New Throw (std::bad_alloc);    ...};
View Code

Because of the name masking rules, base and its derived classes will not be able to use operator new in the global scope:

// operator New in the scope of the standard library global void operator New Throw (std::bad_alloc); void operator New (std::size_t,voidthrow(); void operator New (std::size_t,constthrow();
View Code

To use these forms, you only need to make the class-specific version call the global version, and you must note the correspondence of operator new AND operator Delete:

classstandardnewdeleteforms{ Public:    //Normal New/delete    Static void*operator New(std::size_t size)Throw(std::bad_alloc) {return::operator New(size); }    Static void operator Delete(void* pmemory)Throw(){        return::operator Delete(pmemory); }    //Placement Mew/delete     Static void*operator New(std::size_t size,void* ptr)Throw(std::bad_alloc) {return::operator New(SIZE,PTR); }    Static void operator Delete(void* Pmemory,void* ptr)Throw(){        return::operator Delete(PMEMORY,PTR); }    //nothrow New/delete    Static void*operator New(std::size_t size,Conststd::nothrow_t& NT)Throw(std::bad_alloc) {return::operator New(SIZE,NT); }    Static void operator Delete(void* Pmemory,Conststd::nothrow_t& NT)Throw(){        return::operator Delete(pmemory); }    ...};
View Code

Customers who want to expand the standard form in a custom form can use the inheritance mechanism and the using declarative form to obtain the standard forms:

classWidgets: Publicstandardnewdeleteforms{ Public:    usingStandardnewdeleteforms::operator New; usingStandardnewdeleteforms::operator Delete; Static void*operator New(std::size_t size,std::ostream& LogStream)Throw(Std::bad_alloc); Static void operator Delete(void* pmemory,std::ostream& LogStream)Throw(); ...};
View Code

Effective C + + clause 52 wrote placement new also to write placment delete

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.