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
c++-faq
is not for every self-answered book-style Q&A that a regular user can think of. – Octuplec++-faq
tag was just the first-best crutch that came along, meant to be a temporary solution until we found a better one. I'd be very glad if some new ideas on how to do this would come up and could be discussed. – Valoniah