Violet Giraffe
Violet Giraffe

Reputation: 33579

Why is deleting null pointer allowed in C++

I remember reading somewhere that it's neccessary for delete NULL to be a valid operation in C++, but I can't remeber the reason why it should be so. Would someone please remind me?

Upvotes: 8

Views: 1263

Answers (7)

NullPoiиteя
NullPoiиteя

Reputation: 57322

Deleting a null pointer has no effect (if the deallocation function is one supplied in the standard library[2]), so it is not necessary to check for a null pointer before calling delete.

also check Is it safe to delete a NULL pointer? it may help you

Upvotes: 1

Puppy
Puppy

Reputation: 146910

Because the library implementer just has to write if (ptr == nullptr) return; once. You, the user, would have to write it 9999999999 times throughout your program. Thus, it's a simple case that doing it inside delete is much simpler.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 476980

First off, NULL is never a valid pointer value (in a hosted C++ program), and so there's no ambiguity as to whether the pointer points to a live object or not. Second, the internal memory management logic must do its own checking anyway to do the bookkeeping, so presumably nothing would be gained from mandating that the pointer be non-null.

So now we know that there's nothing speaking against this rule, here's the big argument in its favour: It makes it much easier to write code. Consider this simple exception-handling piece of allocation code:

T * p1 = NULL;
T * p2 = NULL;
T * p3 = NULL;

try
{
    p1 = new T;
    p2 = new T;
    p3 = new T;
}
catch (...)
{
    delete p1;
    delete p2;
    delete p3;

    throw;
}

This is simple and uncluttered. If we needed to add NULL-checks everywhere, it would make the code a lot less readable and obscure the code's logic.

Upvotes: 3

Ed Swangren
Ed Swangren

Reputation: 124642

The rule is not strictly necessary in that the language could exist without it; it is simply a decision made by the standards committee. The null pointer is not a valid memory address. However, I would like to believe that each decision is made for a reason, and those reasons are worth knowing.

This rule simplifies the management of failure cases and other instances in which a pointer may be null. It is an easy, inexpensive check to make, and it adds appreciable convenience to the language.

For example, if many conditions exist which would result in dynamic memory allocation, but others also exist which will not, it's convenient to be able to just stick a delete at the end and not worry about it.

// contrived example
void foo(int n) {
    char *p = nullptr;
    switch(n) {
        case whatever:
        case something_else:
            p = new char[n];
            break;
    }

    // some code...

    delete [] p;  // look Ma, no check!
}

If delete nullptr were illegal then every call to delete would be surrounded by...

if(ptr)

...and that would be lame. Since this would be the norm, an essentially mandatory convention, why not just eliminate the need to check entirely? Why require an extra 5 characters (minimum) for every call to delete?

Upvotes: 11

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145239

Well, I do remember clearly that the rules for deleting pointer-to-const (now OK) changed with the standardization, i.e. they were different in the ARM, the Annotated Reference Manual.

But I'm not sure about deleting 0; I think it's always been supported.

Anyway, it's just about convenience, for an operation where the cost of checking is insignificant, and where it can possibly/likely be done more efficiently by the compiler than by some user defined wrapper function for deletion.

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361342

Because the Standard committee know that no program can have NULL to point to a valid object, i.e NULL cannot point to a valid memory so it is safe to write delete NULL , precisly because it doesn't actually delete anything. Since that is safe, then it saves you from the need to check for NULL , before delete:

//if (ptr != NULL) NOT NEEDED
   delete ptr; //safe even if ptr == NULL

Upvotes: 2

Alok Save
Alok Save

Reputation: 206518

Exact Reason:

  • Because C++ Standard committee decided so.

Possible Reasoning:

  • Because it would be too much code bloat to check every pointer for NULL before calling delete in user code.

Upvotes: 13

Related Questions