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

14,480

Solution 1

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


Solution 2

Part II

... continued

Given the behavior of operator new from the example, a well designed new_handler must do one of the following:

Make more memory available: This may allows the next memory allocation attempt inside operator new's loop to succeed. One way to implement this is to allocate a large block of memory at program start-up, then release it for use in the program the first time the new-handler is invoked.

Install a different new-handler: If the current new-handler can't make any more memory available, and of there is another new-handler that can, then the current new-handler can install the other new-handler in its place (by calling set_new_handler). The next time operator new calls the new-handler function, it will get the one most recently installed.

(A variation on this theme is for a new-handler to modify its own behavior, so the next time it's invoked, it does something different. One way to achieve this is to have the new-handler modify static, namespace-specific, or global data that affects the new-handler's behavior.)

Uninstall the new-handler: This is done by passing a null pointer to set_new_handler. With no new-handler installed, operator new will throw an exception ((convertible to) std::bad_alloc) when memory allocation is unsuccessful.

Throw an exception convertible to std::bad_alloc. Such exceptions are not be caught by operator new, but will propagate to the site originating the request for memory.

Not return: By calling abort or exit.

To implement an class-specific new_handler we have to provide a class with its own versions of set_new_handler and operator new. The class's set_new_handler allows clients to specify the new-handler for the class (exactly like the standard set_new_handlerallows clients to specify the global new-handler). The class's operator new ensures that the class-specific new-handler is used in place of the global new-handler when memory for class objects is allocated.


Now that we understand new_handler & set_new_handler better we are able to modify the Requirement #4 suitably as:

Requirement #4 (Enhanced):
Our operator new should try to allocate memory more than once, calling the new-handling function after each failure. The assumption here is that the new-handling function might be able to do something to free up some memory. Only when the pointer to the new-handling function is null does operator new throw an exception.

As promised, the citation from the Standard:
Section 3.7.4.1.3:

An allocation function that fails to allocate storage can invoke the currently installed new_handler(18.4.2.2), if any. [Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (18.4.2.3).] If an allocation function declared with an empty exception-specification (15.4), throw(), fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall only indicate failure by throw-ing an exception of class std::bad_alloc (18.4.2.1) or a class derived from std::bad_alloc.

Armed with the #4 requirements, let us attempt the pseudo code for our new operator:

void * operator new(std::size_t size) throw(std::bad_alloc)
{  
   // custom operator new might take additional params(3.7.3.1.1)

    using namespace std;                 
    if (size == 0)                     // handle 0-byte requests
    {                     
        size = 1;                      // by treating them as
    }                                  // 1-byte requests

    while (true) 
    {
        //attempt to allocate size bytes;

        //if (the allocation was successful)

        //return (a pointer to the memory);

        //allocation was unsuccessful; find out what the current new-handling function is (see below)
        new_handler globalHandler = set_new_handler(0);

        set_new_handler(globalHandler);


        if (globalHandler)             //If new_hander is registered call it
             (*globalHandler)();
        else 
             throw std::bad_alloc();   //No handler is registered throw an exception

    }

}

Continuation 2

Solution 3

Part III

... continued

Note that we cannot get the new handler function pointer directly, we have to call set_new_handler to find out what it is. This is crude but effective, at least for single-threaded code. In a multithreaded environment, probably some kind of lock to safely manipulate the (global) data structures behind the new-handling function will be needed. (More citation/details are welcome on this.)

Also, we have an infinite loop and the only way out of the loop is for memory to be successfully allocated, or for the new-handling function to do one of the things we inferred before. Unless the new_handler does one of those things, this loop inside new operator will never terminate.

A caveat: Note that the standard (§3.7.4.1.3, quoted above) does not explicitly say that the overloaded new operator must implement an infinite loop, but it merely says that such is the default behaviour. So this detail is open to interpretation, but most of the compilers (GCC and Microsoft Visual C++) do implement this loop functionality (you can compile the code samples provided earlier). Also, since an C++ authory such as Scott Meyers suggests this approach, it is reasonable enough.

Special scenarios

Let us consider the following scenario.

class Base
{
    public:
        static void * operator new(std::size_t size) throw(std::bad_alloc);
};

class Derived: public Base
{
   //Derived doesn't declare operator new
};

int main()
{
    // This calls Base::operator new!
    Derived *p = new Derived;

    return 0;
}

As this FAQ, explains, a common reason for writing a custom memory manager is to optimize allocation for objects of a specific class, not for a class or any of its derived classes, which basically means that our operator new for the Base class is typically tuned for objects of size sizeof(Base) -nothing larger and nothing smaller.

In the above sample, because of inheritance the derived class Derived inherits the new operator of the Base class. This makes calling operator new in a base class to allocate memory for an object of a derived class possible. The best way for our operator new to handle this situation is to divert such calls requesting the "wrong" amount of memory to the standard operator new, like this:

void * Base::operator new(std::size_t size) throw(std::bad_alloc)
{
    if (size != sizeof(Base))          // If size is "wrong,", that is, != sizeof Base class
    {
         return ::operator new(size);  // Let std::new handle this request
    }
    else
    {
         //Our implementation
    }
}

Note that, the check for size also incoprporates our requirement #3. This is because all freestanding objects have a non-zero size in C++, so sizeof(Base) can never be zero, so if size is zero, the request will be forwarded to ::operator new, and it is gauranteed that it will handle it in standard compliant way.

Citation: From the creator of C++ himself, Dr Bjarne Stroustrup.

Solution 4

Implementing a custom delete operator

The C++ Standard(§18.4.1.1) library defines operator delete as:

void operator delete(void*) throw();

Let us repeat the exercise of gathering the requirements for writing our custom operator delete:

Requirement #1: It shall return void and its first parameter shall be void*. A custom delete operator can have more than one parameter as well but well we just need one parameter to pass the pointer pointing to the allocated memory.

Citation from the C++ Standard:

Section §3.7.3.2.2:

"Each deallocation function shall return void and its first parameter shall be void*. A deallocation function can have more than one parameter....."

Requirement #2: It should guarantee that it is safe to delete a null pointer passed as an argument.

Citation from C++ Standard: Section §3.7.3.2.3:

The value of the first argument supplied to one of the deallocation functions provided in the standard library may be a null pointer value; if so, the call to the deallocation function has no effect. Otherwise, the value supplied to operator delete(void*) in the standard library shall be one of the values returned by a previous invocation of either operator new(size_t) or operator new(size_t, const std::nothrow_t&) in the standard library, and the value supplied to operator delete[](void*) in the standard library shall be one of the values returned by a previous invocation of either operator new[](size_t) or operator new[](size_t, const std::nothrow_t&) in the standard library.

Requirement #3: If the pointer being passed is not null, then the delete operator should deallocate the dynamic memory allocated and assigned to the pointer.

Citation from C++ Standard: Section §3.7.3.2.4:

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, render-ing invalid all pointers referring to any part of the deallocated storage.

Requirement #4: Also, since our class-specific operator new forwards requests of the "wrong" size to ::operator new, We MUST forward "wrongly sized" deletion requests to ::operator delete.

So based on the requirements we summarized above here is an standard conformant pseudo code for a custom delete operator:

class Base
{
    public:
        //Same as before
        static void * operator new(std::size_t size) throw(std::bad_alloc);
        //delete declaration
        static void operator delete(void *rawMemory, std::size_t size) throw();

        void Base::operator delete(void *rawMemory, std::size_t size) throw()
        {
            if (rawMemory == 0)
            {
                return;                            // No-Op is null pointer
            }

            if (size != sizeof(Base))
            {
                // if size is "wrong,"
                ::operator delete(rawMemory);      //Delegate to std::delete
                return;
            }
            //If we reach here means we have correct sized pointer for deallocation
            //deallocate the memory pointed to by rawMemory;

            return;
        }
};
Share:
14,480
Alok Save
Author by

Alok Save

Alok is a long time C &amp; C++ enthusiast.He wrote his first commercial C++ program more than a decade ago and has been in love with C++ ever since. He is known to dabble in Java,Pro-C,SQL,PLSQL,Unix scripting &amp; many more languages.He has worked in various technical roles ranging from a programmer to a technical solutions architect.In his spare time he loves answering questions,especially on C &amp; C++. -:Alok's LinkedIn:- SO Milestones: c++ Gold Badge (18/07/2011) c Gold Badge (02/01/2011) legendary Legendary Badge (29/05/2012) Some personal favorite answers:- Why I can't initialize non-const static member or static array in class? When to mark a function in C++ as a virtual? Previous definition error Destructor not invoked when an exception is thrown in the constructor Contribution to c++-faq :- What are Access specifiers? Should I inherit with private,protected or public? Why should one replace default new and delete operators? How should I write ISO C++ Standard conformant custom new and delete operators? What is this weird colon-member syntax in the constructor? What is the difference between char a[] = "string"; and "char *p = string;" External links he likes/recommends: C++ Faq Bjarne Stroustrup's C++ Style and Technique FAQ Bjarne Stroustrup's general faq C-Faq Clockwise Spiral Rule What Every Programmer Should Know About Floating-Point Arithmetic

Updated on June 03, 2022

Comments

  • Alok Save
    Alok Save about 2 years

    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

    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.*
  • Sjoerd
    Sjoerd almost 13 years
    I fail to see what "Deinstall the new-handler" has to do with an answer to "How should I write a C++ new operator".
  • Alok Save
    Alok Save almost 13 years
    @Sjoerd: You will need to read the answer to know how it is organized.
  • Sjoerd
    Sjoerd almost 13 years
    Please quote where in the standard it is stated that a user supplied new operator has to call the new-handler. If it is not a requirement on an user defined operator new, what is it doing in this (far too long) answer?!
  • Alok Save
    Alok Save almost 13 years
    @Sjoerd: All the citations from the Standard are present in there you will need to read to understand it.
  • Sjoerd
    Sjoerd almost 13 years
    Your references are to the C++98 standard, not the current C++11 standard.
  • R. Martinho Fernandes
    R. Martinho Fernandes almost 13 years
    @Sjoerd: As of this writing, the current standard is still C++03. But if you want one from the C++11 approved draft, the paragraph number is the same.
  • Sjoerd
    Sjoerd almost 13 years
    Quote: "An allocation function that fails to allocate storage can invoke the currently installed new-handler function (18.6.2.3), if any." note the "CAN". Not a requirement.
  • Alok Save
    Alok Save almost 13 years
    @Sjoerd: C++11, is not an standard yet, not officially atleast. So the official standard at the moment is still C++03. I won't mind adding the relevant C++11 quotes as I track them.
  • R. Martinho Fernandes
    R. Martinho Fernandes almost 13 years
    @Sjoerd: "Our operator new should try to allocate memory more than once (...)". Also note the "SHOULD". Not a requirement.
  • Sjoerd
    Sjoerd almost 13 years
    The new standard has been approved, it will be published in a couple of weeks. Do you really want to rewrite all the references in this post in two weeks time? Src: herbsutter.com/2011/08/12/…
  • Lightness Races in Orbit
    Lightness Races in Orbit almost 13 years
    @Sjoerd: The FDIS was approved. It is not a standard until it is published. When Herb says "it is now C++11", he was lying. All we have is the C++0x FDIS, which is identical in content to what will be the C++11 standard in a few weeks.
  • Sjoerd
    Sjoerd almost 13 years
    And my point stands: Calling the new-handler is not a requirement on a user-supplied operator new function, so it should not be part of an answer to the question. Just list the 4 requirement of the first part of the answer, and a conforming implementation as short as possible. Could be half a page, at most.
  • sbi
    sbi almost 13 years
    @Sjoerd: Please quote where in the answer it is stated that a user supplied new operator has to call the new-handler. If it is not written in the answer, what is it doing in your (far too long) comment complaints?!
  • Martin York
    Martin York almost 13 years
    Personally I would save the result of std::set_new_handler. Then my version of new handler would call the old version if my version failed to provide any emergency space. This way if another library has installed a new handler that will be called as expected by that library.
  • Kerrek SB
    Kerrek SB almost 13 years
    Are you sure new is in namespace std?
  • Sjoerd
    Sjoerd almost 13 years
    @sbi The question is about an user defined standard compliant operator new. If the new_handler does not have to be called, why is it in the answer at all? It only distracts.
  • Sjoerd
    Sjoerd almost 13 years
    @sbi I don't see why you SHOULD call the new_handler. of course, one COULD call it, but one could also call a lot of other functions. And that argument has been the same since my first comment, so I don't see where I shifted goalpost. Strawman argument, you didn't have anything better? The answer is now too long to be useful, and I was looking for ways to shorten it to make it more useful. But I have stated my opinion enough times here, and you still fail to see my point so it's pointless to continue. And BTW, calling me a troll is offensive and rude (and flagged as such).
  • sbi
    sbi almost 13 years
    @Sjoerd: Your issues ranged from "why talk about deinstalling the new-handler" via "a user supplied new operator does not have to call the new-handler" to "which is the current C++ standard". All your arguments in these have been shown to be false by Als and several bystanders. Now you come back with that you don't see why a new-handler should be called - something you had complained about before.
  • sbi
    sbi almost 13 years
    However, I agree to your question to the extend that Als could make it clearer why he thinks a user-defined new operator should fall back onto a new handler. Had you asked this immediately and politely, we would have discussed this, rather than bickering with you, and would be a lot further by now.
  • sbi
    sbi almost 13 years
    As for the length of the answer: Why don't you suggest how to break it into smaller, independent parts? That might indeed be more useful.
  • lmat - Reinstate Monica
    lmat - Reinstate Monica over 10 years
    I read this whole post for the "deallocate the memory pointed to by rawMemory" part... should I use free and just assume that the default operator new used malloc (or whatever) ?
  • Jimmio92
    Jimmio92 almost 3 years
    100,000,000 * 4 bytes = 400,000,000 bytes / 1024 = 390625 KiB / 1024 = ~381.47 MiB. Most likely will not fail on anything you could view this webpage on :)