[Effective C + +] customizing new and delete

Source: Internet
Author: User

The two main characters in this article are the assignment routines and the return routines (allocation and deallocation routines, which is operator new and operator delete), and the supporting role is New_handler, which is when operator The function called when new does not meet the client's memory requirements.

The heap memory used by the STL container is managed by the allocator object owned by the container (allocator objects), not directly managed by new and delete.

Article 49: Understand New-handler's behavior (understand the behavior of the New-handler.)

Please remember:

1) Set_new_handler allows the client to specify a function that is called when the memory allocation is not met.

2) Nothrow New is a fairly limited tool because it only applies to memory allocations, and subsequent constructor calls may throw exceptions.

Article 50: Understanding the appropriate replacement time for new and delete (Understand when it makes sense to repace new and delete.)

Keep in mind that there are many reasons to write custom new and delete, including improving performance, debugging heap errors, and collecting heap usage information.

Article 51: To write new and delete when you need to adhere to the general (adhere to Convention when writing new and delete.)

Please remember:

1) operator new should contain an infinite loop in which to attempt to allocate memory, and if it fails to meet memory requirements, the call is New-handler. It should also be able to handle 0 bytes applications. The class-specific version should also handle "larger (error) applications than the correct size."

2) Operater Delete should not do anything when a null pointer is received. The class-specific version should also handle "larger (error) applications than the correct size."

clause 52: Write placement new also to write placement delete (Write placement Delete if you Write placement new.)

Please remember:

1) When you write a placement operator new, make sure that the corresponding placement operator delete is also written. If you do not do this, your program may be exposed to a memory leak that is slightly intermittent.

2) When you declare placement new and placement Delete, make sure that you do not inadvertently obscure their normal version.

[Effective C + +] customizing new and 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.