Article 8: When writing operator new and operator Delete, follow the regular rules

Source: Internet
Author: User
Tags class operator

When you rewrite operator new by yourself (cla10 explains why you sometimes rewrite it), it is important that the behavior provided by the function should be consistent with the default operator new of the system. In practice, the correct return value must be obtained. When the available memory is insufficient, the error processing function must be called (see Article 7). The 0-byte memory request must be processed. In addition, we should avoid hiding the standard form of new accidentally, but this is the topic of Clause 9.

The return value is simple. If the memory allocation request is successful, a pointer to the memory is returned. If the request fails, a STD: bad_alloc type exception is thrown in accordance with Clause 7. Operator new actually tries to allocate memory more than once. It needs to call the error processing function after each failure and expects the error processing function to release memory elsewhere. Operator new throws an exception only when the pointer to the error processing function is null.

The C ++ standard requires that operator new returns a valid pointer even when the request is allocated 0 bytes of memory.

The pseudocode of operator new in the form of non-class members looks like the following:

Void * operator new (size_t size) // operator new may also have other parameters {If (size = 0) {// when processing 0-byte requests, size = 1; // treat it as one byte request} while (1) {allocate size byte memory; If (allocated successfully) Return (pointing to memory pointer ); // if the allocation fails, find the current error handler new_handler globalhandler = set_new_handler (0); set_new_handler (globalhandler); If (globalhandler) (* globalhandler) (); else throw STD :: bad_alloc ();}}

 

The trick to process a zero-byte request is to process the request as one byte.

Operator new often inherits quilt classes, which leads to some complexity. Most operator New for classes (including those in Clause 10) are designed only for specific classes, not for all classes, nor for all their subclasses. This means that for operator new of Class X, the internal behavior of the function is precise sizeof (x) when it comes to objects: Not big or small. However, due to inheritance, operator new in the base class may be called to allocate memory for a subclass object:

Class base {public: static void * operator new (size_t size );...}; class derived: public base // The derived class does not declare operator new {...}; // derived * P = new derived; // call base: Operator new

If the base-class operator new doesn't need to work hard to handle this situation-this is unlikely to happen-the simplest way is to transfer the memory allocation requests with this "error" quantity to the standard operator new to handle, as follows:

Void * base: Operator new (size_t size) {If (size! = Sizeof (base) // if the number is "Incorrect", let the standard operator new return: Operator new (size); // process this request //... // otherwise process this request}

To control the memory allocation of class-based arrays, you must implement the array form of operator New-operator new []

All the conventions to be followed when operator new (and operator new []) is rewritten. Operator Delete (and its partner operator Delete []) is simpler. All you need to remember is that C ++ ensures that deleting null pointers is always safe, so you must fully apply this guarantee. The following is a pseudo code of operator Delete, which is a non-class member:

Void operator Delete (void * rawmemory) {If (rawmemory = 0) return; // if the pointer is null, return // release the memory pointed to by rawmemory; return ;}

Assume that operator new of the class transfers the allocation request of "error" size to: Operator new, the deletion request of "error" size must also be transferred to: Operator delete:

Class base {// is the same as above, but public: // operator Delete static void * operator new (size_t size); static void operator Delete (void * rawmemory, size_t size );...}; void base: Operator Delete (void * rawmemory, size_t size) {If (rawmemory = 0) return; // check the NULL pointer if (size! = Sizeof (base) {// If size "is incorrect",: Operator Delete (rawmemory); // Let the standard operator handle the request return;} release the memory pointing to rawmemory; return ;}

 

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.