Item52 of Reading Notes in Objective C ++: If you write placement new, you also need to write placement Delete.

Source: Internet
Author: User

1. when a placement new (New at a specific position) is declared in the class, it accepts other variables except STD: size_t as parameters, A placement delete must also be declared and its parameters are the same as those of placement new to get a corresponding relationship, in this way, when the memory allocation fails, the C ++ compiler will call the placement delete that matches the placement new to return the memory.

Note: Placement Delete is called only when an exception occurs in the "Constructor triggered with the call to placement New". It is not called when you delete an existing pointer.

2. Avoid using the class-specific new to cover up other new types that the customer expects, such as the general version of new or the new in its base class. The method is to use the using declarative in the appropriate scope.

By default, C ++ provides three types of operator new in the global scope:

  void  *  operator   New  (STD: size_t)  throw  (STD: bad_alloc);  // normal New   void  *  operator   New  (STD: size_t,  void  *)  throw  ();  // placement new   void  *  operator   New  (STD: size_t,  const  STD: nothrow_t &)  throw  ();  // nothrow new  

If the exclusive operator new is declared in the class, the above version is concealed. In general, these functions should be guaranteed to be available outside the class, and the pair operator new and operator Delete should be declared at the same time. You can declare a base class that contains all the above functions and their corresponding operator Delete functions. You can define a class to inherit the base class and use the using declarative method to obtain the standard form, so that it does not conflict with the custom version.

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.