35. c ++ primer 4th notes, special tools and technologies, optimized memory allocation (2)

Source: Internet
Author: User

9, Just as positioningNew (placement new expression)Expression is usedAllocatorClassConstructLow-level selection of members,You can call the Destructor explicitly.DestroyLow-level selection of functions.

ExampleCode

 
For (T * P = first_free; P! = Elements;/* empty */) alloc. Destroy (-- p); // The upper expression is equivalent to the lower expression ~ T ();

10Explicitly calling the Destructor is to clear the object itself, and does not release the memory occupied by the object; callingOperator DeleteThe function does not run the destructor. It only releases the specified memory.

11Class by defining its own nameOperator newAndOperator DeleteCan be used to manage memory of its own type.

The compiler sees the class type ( For a class type ) New or Delete , it checks whether the class has operator new or operator Delete member, if the class defines (or inherits) its own member New and Delete function, the functions are used to allocate and release memory for the object; otherwise, the standard library version is used.

OptimizationNewAndDeleteYou only need to defineOperator newAndOperator DeleteNew version,NewAndDeleteThe expression removes the construction of a self-care object.

12, In the customNewAndDeleteIn pairs. Use all the custom and global.

You can use the global scope to determine the operator to forceNew,DeleteThe expression uses a global library function.

Sample Code

 
Type * P =: new type; // uses global operator New: Delete P; // uses global operator Delete

13If the base class hasVirtualThe Destructor is passedOperator DeleteThe size varies according to the dynamic type of the object indicated by the deleted pointer.VirtualDestructor, then, the behavior of deleting the pointer pointing to the object of the derived class through the base class pointer is as undefined as usual.

These functions (Operator new And Operator Delete ) Is implicitly a static function and does not need to be explicitly declared Static . Member New And Delete Functions must be static, because they must be used before the object is constructed ( Operator new ), Or use ( Operator Delete ). Therefore, these functions have no member data to manipulate. Like any other static member function,NewAndDeleteYou can only directly access static members of the class.

14 A general policy is to pre-allocate an original memory to save unconstructed objects. when creating new elements, they can be constructed in a pre-allocated object; when releasing elements, place them back in the pre-allocated object block instead of returning the memory to the system. This policy is often called to maintain a free list. The Free List can be implemented as a linked list of allocated but unconstructed objects.

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.