Why deleting a null pointer is allowed in C ++

I remember reading somewhere that it is necessary to have a valid operation in C ++ for delete NULL , but I cannot remember why this is so. Will someone please remind me?

+8
c ++ delete-operator
source share
7 answers

A rule is not strictly necessary because a language can exist without it; it is just a decision made by the standards committee. A null pointer is not a valid memory address. However, I would like to believe that every decision is made for some reason, and these reasons deserve attention.

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

For example, if there are many conditions that will lead to the allocation of dynamic memory, but there are others that will not, the convenience will simply insert 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, every call to delete would be surrounded ...

 if(ptr) 

... and that would be lame. Since this will be the norm, essentially a binding convention, why not just eliminate the need to fully verify? Why require an additional 5 characters (minimum) for each delete call?

+11
source share

Exact reason:

  • Because the C ++ Standard committee decided so.

Possible reasoning:

  • Because too much bloating of the code, each pointer checks for NULL before calling delete in the user code.
+14
source share

Firstly, NULL never a valid pointer value (in a hosted C ++ program), and therefore there is no ambiguity as to whether the pointer points to a living object or not. Secondly, the internal memory management logic must do its own check in any case for accounting, so it would probably not work out if the pointer were not null.

So, now we know that he does not say anything against this rule, here is a big argument in his favor: it makes writing code much easier. Consider this simple example of processing 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; } 

It is simple and uncluttered. If we needed to add NULL checks everywhere, this would make the code much less readable and obscure by the logic of the code.

+3
source share

Since the standard commission knows that no program can have NULL to point to a valid object, that is, NULL cannot point to a valid memory, it is therefore safe to write delete NULL , precisely because it doesn’t actually delete anything. Since this is safe, it saves you from having to check for NULL before delete :

 //if (ptr != NULL) NOT NEEDED delete ptr; //safe even if ptr == NULL 
+2
source share

Because the library executor simply has to write if (ptr == nullptr) return; once. You, the user, will have to write it 9999999999 times throughout your program. Thus, it is a simple case when doing this inside delete much simpler.

+2
source share

Deleting a null pointer has no effect (if the release function is included in the standard library [2]), so there is no need to check the null pointer before calling delete.

also check Is it safe to delete the NULL pointer? it can help you

+1
source share

Well, I remember well that the pointer-to- const removal rules (now OK) have changed with standardization, that is, they were different in ARM, in the annotated reference manual.

But I'm not sure about removing 0; I think this has always been supported.

In any case, this is just a convenience for an operation where the verification cost is negligible and where it can / most likely can be performed more efficiently by the compiler than by a specific user-defined wrapper function for deletion.

0
source share

All Articles