Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:55:26+00:00 2026-05-24T22:55:26+00:00

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

  • 0

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. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:55:27+00:00Added an answer on May 24, 2026 at 10:55 pm

    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


    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why should would one replace the default operator new and delete with a custom
Ive got this function as a html helper which should write a javascript function
How I should write: long.MaxValue or Int64.MaxValue ? Are there standard from Microsoft?
This function should write the time spent on the page to the div. It
I learned from books that you should write for loop like this: for(var i=0,
Are there guidelines on how one should write new container which will behave like
I'm kind of new to SQL Server/C#. My teacher thought I should write data
Should there be any specific order in which I should write the following for
i want to upload the sound file (.ogg) in server. But it should write
Question: Should I write my application to directly access a database Image Repository or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.