Exercise tive C ++ item51: Stick to the general rules when writing new and delete books

Source: Internet
Author: User

1. When you customize operator new, the following requirements should be met:

(1) operator new should contain an infinite loop and try to allocate memory in it. If the allocation fails, it should call New-handler.

(2) At the same time, it should be able to process the 0-byte application. A common practice is to regard it as a 1-byte application. C ++ requires that operator new should return a valid pointer even if the customer requires 0 bytes.

(3) operator new of the class exclusive version should handle "Requests larger than the correct size (error ". Note that the memory allocation scheme applicable to the base class may not apply to the derived class. If a class is inherited, its derived class may call the operator new of the base class, the method to prevent this is to add a judgment in the function: Determine whether the class is the base class (compare and judge by sizeof (). If not, call the global version.

PS: I think calling the global version may not be the best method. The reason for doing so may be that you should consider not adding the exclusive operator new to the derived class, if you are not afraid of hard work, you can design a memory solution for the derived class.

Example:

Void * operator new (STD: size_t size) Throw (STD: bad_alloc) {using namespace STD; If (size = 0) {// if the customer requests 0-bit memory, the memory size is automatically converted to 1 bit;} while (true ){... // Infinite loop, try to allocate size bytesif (...) // Return ...; // A pointer pointing to the allocated memory // allocation failed. Find the current new-handler function new_handler globalhandler = set_new_handler (0); set_new_handler (globalhandler ); // these two steps are used to obtain the new-handler function pointer if (globalhandler) (* globalhandler) (); else throw STD: bad_alloc ();}

2. If operator new [] is designed, allocate as much memory as possible.

3. If operator Delete is designed, you should ensure that "deleting null pointers is always safe" by checking whether the pointer is null in this function.

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.