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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:48:54+00:00 2026-05-17T01:48:54+00:00

While reading answers to this question I noticed that answers ( this for example)

  • 0

While reading answers to this question I noticed that answers (this for example) imply that operator delete can be called even when delete statement is executed on a null pointer.

So I wrote a small snippet:

class Test {
public:
    void* operator new( size_t ) { /*doesn't matter*/ return 0; }
    void operator delete( void* ptr ) {
        ptr; //to suppress warning and have a line to put breakpoint on
    }
};

int main()
{
    Test* ptr = 0;
    delete ptr;
}

and – surprisingly for me – Test::operator delete() is invoked with ptr holding a null pointer.

As I understand it operator new allocates memory and operator delete returns memory to the allocator. If I call delete statement on a null pointer it means there was no object behind the pointer and there’s no memory to return to the allocator.

delete statement includes invoking a destructor. When I pass a null pointer the destructor is surely not invoked – C++ takes care of that. Then why is operator delete invoked in this case?

  • 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-17T01:48:55+00:00Added an answer on May 17, 2026 at 1:48 am

    The language in the upcoming C++0x standard (section 5.3.5 [expr.delete]) is as follows:

    If the value of the operand of
    the delete-expression is not a null
    pointer value, the delete-expression
    will call a deallocation function
    (3.7.4.2). Otherwise, it is
    unspecified whether the deallocation
    function will be called. [ Note: The
    deallocation function is called
    regardless of whether the destructor
    for the object or some element of the
    array throws an exception. — end note ]

    So it is unspecified behavior, some compilers may call operator delete when a NULL pointer is deleted and others may not.

    EDIT: The term deallocation function used by the standard seems to be causing some confusion. It comes with a reference. Some key language from 3.7.4.2 [basic.stc.dynamic.deallocation] which may help clarify:

    If a class T has a member deallocation function named operator delete
    with exactly one parameter, then that function is a usual (non-placement) deallocation function.

    The standard is also very clear that user-defined operator delete needs to accept a parameter which is a null pointer value:

    The value of the
    first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation
    function is one supplied in the standard library, the call has no effect.

    But because of the unspecified behavior 5.3.5, you shouldn’t rely on your operator delete being called when the pointer is null.

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

Sidebar

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.