About C ++ placement new and placement Delete

Source: Internet
Author: User

I posted a question yesterday and summarized it here today. Click to enter the original post. If you are interested, you can check it out. I will talk about it later.

C ++'s new is a language-defined operator. The operator's behavior contains two things that you cannot change.

First thing: Call operator new to allocate memory. Therefore, the overload of the new function is actually operator new, and you cannot overload the new function.

The second thing: Call the constructor of the class of the object to be new to initialize the memory allocated in the first step. This is why you can directly use pobj-> call functions in the class after object * pobj = new object.

The delete operator is similar, but the Destructor is called first and then the memory is released. There is an operator Delete.

The purpose of the code in the post is to reload operator new and operator Delete and record the history of new and delete through an ostream. Now, the code is displayed:

# Include <iostream> using namespace STD; Class B {public: B () {cout <"ctor B" <Endl ;}~ B () {cout <"dtor B" <Endl;} void * operator new (size_t size, ostream & out) {out <"New B" <Endl; return: Operator new (size);} void operator Delete (void * MEM, ostream & out) {out <"delete B" <Endl ;:: operator Delete (MEM) ;}}; int main () {B * pb = new (cout) B; B * master =: New B; // Delete Pb; this statement fails to be compiled. No suitable 'operatordelete' for 'B': delete k22 ;}

If there is no problem in commenting, the output of this Code (expected) is:

new Bctor Bctor Bdtor Bdelete Bdtor B

Class B is not explained. Let's look at the main function directly:

B * pb = new (cout) B; this sentence is a new B object. Because operator new is overloaded in Class B, an ostream object needs to be passed in, it is passed like this. If you call B * pb = new B in this way, an error is returned, because operator new in Class B hides the default value. Output of this sentence: New B ctorb.

B * PBS =: New B; this sentence is also a new B object, but there are two additional colons in front of new, which means to call the global new, the operator new with the hidden call mentioned above is called. Output of this sentence: ctor B.

Delete Pb; this statement deletes the object pointed to by Pb, but fails because it is later. Expected output: dtor B Delete B.

: Delete master; Delete the object to which the master node points. Call the global delete operation. The output is dtor B.

Why is the delete Pb failure:

2 floor rabbitlbj:

First, it is better to change the new and delete statement to static and change the wrong row to B: Operator Delete (Pb, cout); then you can call placement Delete, however, there is a gap between the idea and the landlord. This function directly releases the memory and no longer analyzes the structure. It seems that this Delete cannot be called outside through the delete keyword, this is generally called for the system when a placement new error occurs. As long as placement new is used, the analysis structure is generally manual.

4 floor pengzhixi:

C ++ does not have the built-in "placement new" because it cannot provide a general purpose. B * pb = new (cout) B; when we write such code, C ++ cannot introduce the object type stored in the Pb pointer. (Although we know what type it is ). So there will be no placement Delete. the compiler will call placement Delete only when placement new throws an exception. In this case, the compiler only needs to release the memory because the construction is unsuccessful. Therefore, LZ can only display and call the Destructor first, and then call placement Delete in the display. In addition, do not reload operator new operator Delete.

The two friends explained this clearly and I will not draw any more. I would like to thank these two friends for their answers.

Reference:

Scott Meyers. Valid tive C ++ 3rd edition. 2005

Scott Meyers. More valid tive C ++. 1996

Related Article

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.