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 6718673
In Process

The Archive Base Latest Questions

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

I’ve been reading about overloading new and delete (and related topics like placement new/delete).

  • 0

I’ve been reading about overloading new and delete (and related topics like placement new/delete). One thing that is confusing me thus far is that operator delete’s standard signature is (at class-scope):

void operator delete(void *rawMemory, std::size_t size) throw();

Delete is called like this:

MyClass* ptr = new MyClass;
delete ptr;

So, how does delete ptr; provide the second parameter for size? Also, can I assume that the MyClass* is implicitly converted to the void* in this circumstance?

  • 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-26T08:59:56+00:00Added an answer on May 26, 2026 at 8:59 am

    Short Answer:

    new and delete operators are overloaded at class scope for optimizing allocation for objects of specific class. But there can be special scenarios due to certain beasts like Inheritance which may lead to allocation requests for more than the class size itself,Since the very purpose of new and delete overload is special tuning for objects of size sizeof(Base), nothing larger or smaller, these overloaded operators should forward all other wrong sized memory requests to ::operator new and ::operator delete, to be able to do so, the size parameter needs to be passed as an parameter.

    Long Answer:

    Consider a Special 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;
    }
    

    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," i.e != sizeof Base class
        {
             return ::operator new(size);  // let std::new handle this request
        }
        else
        {
             //Our implementation
        }
    }
    

    While overloading the delete operator, One must also ensure that since class-specific operator new forwards requests of the “wrong” size to ::operator new, One MUST forward “wrongly sized” deletion requests to ::operator delete, Since the original operators are guaranteed to to handle these requests in a standard compliant manner.

    So the custom delete operator will be something like this:

    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;
         }
    };
    

    Further Reading:
    The following C++-Faq entry talks about overloading new and delete in a standard compliant way and might be a good read for you:

    How should i write iso c++ standard conformant custom new and delete operators?

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a

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.