Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I write ISO C++ Standard conformant custom new and delete operators?

How should I write ISO C++ standard conformant custom new and delete operators?

This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ, Operator overloading, and its follow-up, Why should one replace default new and delete operators?

Section 1: Writing a standard-conformant new operator

  • Part 1: Understanding the requirements for writing a custom new operator
  • Part 2: Understanding the new_handler requirements
  • Part 3: Understanding specific scenario requirements

Section 2: Writing a standard-conformant delete operator

-

Implementing Custom delete operator

_(Note: This is meant to be an entry to [Stack Overflow's C++ FAQ](https://stackoverflow.com/questions/tagged/c++-faq). If you want to critique the idea of providing an FAQ in this form, then [the posting on meta that started all this](https://meta.stackexchange.com/questions/68647/setting-up-a-faq-for-the-c-tag) would be the place to do that. Answers to that question are monitored in the [C++ chatroom](https://chat.stackoverflow.com/rooms/10/c-lounge), where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)_ *Note: The answer is based on learnings from Scott Meyers' More Effective C++ and the ISO C++ Standard.*
like image 833
Alok Save Avatar asked Aug 25 '11 16:08

Alok Save


People also ask

What is the syntax of new and delete operator?

Syntax: // Release memory pointed by pointer-variable delete pointer-variable; Here, pointer-variable is the pointer that points to the data object created by new.

What is new and delete operator in C?

These operators allocate memory for objects from a pool called the free store (also known as the heap). The new operator calls the special function operator new , and the delete operator calls the special function operator delete .

What is the syntax for delete operator?

Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.

What is the use of new and delete operator with example?

- new and delete operators are provided by C++ for runtime memory management. They are used for dynamic allocation and freeing of memory while a program is running. - The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new.


1 Answers

Part I

This C++ FAQ entry explained why one might want to overload new and delete operators for one's own class. This present FAQ tries to explain how one does so in a standard-conforming way.

Implementing a custom new operator

The C++ standard (§18.4.1.1) defines operator new as:

void* operator new (std::size_t size) throw (std::bad_alloc); 

The C++ standard specifies the semantics that custom versions of these operators have to obey in §3.7.3 and §18.4.1

Let us summarize the requirements.

Requirement #1: It should dynamically allocate at least size bytes of memory and return a pointer to the allocated memory. Quote from the C++ standard, section 3.7.4.1.3:

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size...

The standard further imposes:

...The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function). Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a non-null pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was sub-sequently passed to an operator delete.

This gives us further important requirements:

Requirement #2: The memory allocation function we use (usually malloc() or some other custom allocator) should return a suitably aligned pointer to the allocated memory, which can be converted to a pointer of an complete object type and used to access the object.

Requirement #3: Our custom operator new must return a legitimate pointer even when zero bytes are requested.

One of the evident requirements that can even be inferred from new prototype is:

Requirement #4: If new cannot allocate dynamic memory of the requested size, then it should throw an exception of type std::bad_alloc.

But! There is more to that than what meets the eye: If you take a closer look at the new operator documentation (citation from standard follows further down), it states:

If set_new_handler has been used to define a new_handler function, this new_handler function is called by the standard default definition of operator new if it cannot allocate the requested storage by its own.

To understand how our custom new needs to support this requirement, we should understand:

What is the new_handler and set_new_handler?

new_handler is a typedef for a pointer to a function that takes and returns nothing, and set_new_handler is a function that takes and returns a new_handler.

set_new_handler's parameter is a pointer to the function operator new should call if it can't allocate the requested memory. Its return value is a pointer to the previously registered handler function, or null if there was no previous handler.

An opportune moment for an code sample to make things clear:

#include <iostream> #include <cstdlib>  // function to call if operator new can't allocate enough memory or error arises void outOfMemHandler() {     std::cerr << "Unable to satisfy request for memory\n";      std::abort(); }  int main() {     //set the new_handler     std::set_new_handler(outOfMemHandler);      //Request huge memory size, that will cause ::operator new to fail     int *pBigDataArray = new int[100000000L];      return 0; } 

In the above example, operator new (most likely) will be unable to allocate space for 100,000,000 integers, and the function outOfMemHandler() will be called, and the program will abort after issuing an error message.

It is important to note here that when operator new is unable to fulfill a memory request, it calls the new-handler function repeatedly until it can find enough memory or there is no more new handlers. In the above example, unless we call std::abort(), outOfMemHandler() would be called repeatedly. Therefore, the handler should either ensure that the next allocation succeeds, or register another handler, or register no handler, or not return (i.e. terminate the program). If there is no new handler and the allocation fails, the operator will throw an exception.

Continuation 1


like image 170
Alok Save Avatar answered Sep 29 '22 23:09

Alok Save