Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to delete a nullptr twice in C++?

Tags:

c++

I have watched a talk in CPPCon which is back to basic: class layout and the link is this. At 54:20, he said it's undefined bahavior to delete the nullptr twice. As far as I know, C++ standard guarantee deleting a nullptr does nothing but why deleting a nullptr twice is undefined bahavior?

And I was told before that there is no need to check if ptr is null in destructor because deleting a null pointer is valid. But if delete a null pointer twice is undefined, does that mean I still need to check if it's nullptr to prevent double-deleting happen?

This is a transcription of the author from his video:

[...] ignore the standard and then got later problems. A common example I see is it's ok to delete a null pointer, that's fine, but you can't delete it twice without resetting the value to some valid pointer value. If I delete the same pointer twice if it's not null you'll get probably a segfault, if it is null it typically just happens to work, but it's not guaranteed to work and there was actually one compiler in the 1980s where it wouldn't work because when you deleted a pointer a new value was overwritten in the deleted pointer. So again, do follow the standard.

like image 468
Kidsunbo Avatar asked Aug 28 '21 02:08

Kidsunbo


People also ask

Is deleting nullptr safe?

NULL and nullptr will in practice correspond to the same pointer value. It's safe to call delete on null pointers per 6.7.

What happens if you delete nullptr?

What happens when delete is used for a NULL pointer? Explanation: Deleting a null pointer has no effect, so it is not necessary to check for a null pointer before calling delete.

What happens if you delete a pointer twice?

If delete is applied to one of the pointers, then the object's memory is returned to the free store. If we subsequently delete the second pointer, then the free store may be corrupted.

Is it fine to call delete twice for a pointer?

Is it fine to call delete twice for a pointer? Explanation: It is undefined behavior to call delete twice on a pointer. Anything can happen, the program may crash or produce nothing.


1 Answers

Is it safe to delete a nullptr twice in C++?

Yes (in all standard versions of C++). It is guarnteed to work (by work, I mean it doesn't do anyting).

Deletion has no effects if the argument is a null pointer. The compiler that the presenter describes did not conform to the C++ standard. The described compiler also was from the 80's, so it was made before C++ was standardised. The presenter is wrong in saying that you can't delete the null pointer twice if they are referring to standard C++ which does seem to be implied.

It is true that deletion may indirectly cause the program to behave as if the value of the argument pointer was changed (and by as-if rule that means that they effectively can change the value), but only in case where the pointer was non-null and is thus invalidated by the deletion. In fact, this allowance applies to all pointer objects that had the same value as all of them are thereby invalidated. This is because all ways that could observe the value of an invalid pointer are either undefined or implementation defined behaviour.

like image 147
eerorika Avatar answered Nov 15 '22 09:11

eerorika