LoginSignup
2
1

More than 5 years have passed since last update.

C++ではnull pointerをdeleteしても安全だった

Last updated at Posted at 2018-04-20

諸事情でC++のreferenceを見ていて発見した。

If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's implementation-defined), but the default deallocation functions are guaranteed to do nothing when handed a null pointer.
(until C++14)

If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called.
(since C++14)

C++14を境に微妙に動作が異なるが、null pointerをdeleteしても何も起きないようだ。

やったねif文が減らせるよ!

//現在
if(p){
    delete p;
}

//これから
delete p;
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1