2

Is it safe to always omit NULL pointer check before calling delete or delete[], especially while writing cross-platform code sections?

I remember that few years ago (three or four) same code I wrote was working on MS Windows (compiled with MSVCv12 toolchain) but version compiled for Linux with g++ (unfortunately, I do not remember the g++ version) was throwing NULL pointer reference. I found information that it was probably a compiler error at that time.

I've found this SO thread but after reading it I'm still not quite sure if it is safe and if so - from which version of the C++ standard?

elklepo
  • 509
  • 4
  • 17
  • 1
    what does it mean to "throw a NULL pointer reference" ? – 463035818_is_not_an_ai May 11 '18 at 10:13
  • "_I found information that it was probably a compiler error at that time._" How can the compiler check that the pointer being deleted is `nullptr`, and issue an error for it? – Algirdas Preidžius May 11 '18 at 10:17
  • @AlgirdasPreidžius Pretty sure they meant "compiler bug". – Baum mit Augen May 11 '18 at 10:17
  • That's right, I meant "compiler bug" instead of "compiler error". I'm sorry for my mistake. – elklepo May 11 '18 at 10:19
  • [possible duplication?](https://stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer) – xvnm May 11 '18 at 10:20
  • use a smart pointer or vector and you no longer need to worry about the word delete at all – UKMonkey May 11 '18 at 10:29
  • I recall a couple of VERY old C++ compilers (from 1992 or so) in which `delete (any_type *)NULL` or `delete [] (any_type *)NULL` would cause a runtime error. But both those compilers (or their libraries) were fixed in a subsequent release, presumably due to bug reports. I'm aware of no compiler more recent than 1995 which doesn't correctly deal with `delete NULL` - i.e. having no effect. With modern compilers, a crash on any usage of operator `delete` - including `delete NULL` - is generally a symptom of some OTHER preceding code in the program exhibiting undefined behaviour. – Peter May 11 '18 at 10:51

1 Answers1

10

Yes, the standard, since C++98, guarantees that delete or delete[] on a nullpointer has no effect.

C++98 §5.3.5/2

In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.

This was so also before the first standard, when the language was defined by the Annotated Reference Manual.


Regarding

version compiled for Linux with g++ (unfortunately, I do not remember the g++ version) was throwing NULL pointer reference

That's impossible to discuss without a concrete and preferably complete example that reproduces the behavior. It had nothing to do with deleting a nullpointer.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    there is one little detail abou C++11 and later: it is unspecified if deallocation function is called or not if pointer is null, so it would be unsafe if deallocation function is re-implemented by user in way it may cause fault or UB if pointer is nullptr. But it is case of crafting a rake and stepping on it thereafter – Swift - Friday Pie May 11 '18 at 11:12
  • 1
    @Swift -- `operator delete` and `operator delete[]` are required to handle null pointers sensibly. "Requires: [the pointer argument] shall be a null pointer or its value shall be a value returned by an earlier call to [operator new]". [new.delete.single], repeated in [new.delete.array]. If you replace operator delete you are on notice that your version can be called with a null pointer. – Pete Becker May 11 '18 at 12:32
  • @PeteBecker I know that, but some don't. There were questions about that on SO. I just noted that as a source of insecure behaviour in addition to quite fine and upvoted answer. The thing is that when we write delete, it's delete expression which would rely on operator delete supplied by user if one present. Just like with new-expression compiler there acts as mediator. Some imagine that compiler out to fix that, while ISO leaves that undefined – Swift - Friday Pie May 11 '18 at 15:11