意外と知らない人が多いので書いてみた。C++の規約レベルで安全は保障されているので大丈夫。
(中略)if the value of the operand of delete is the null pointer the operation has no effect.ただし、(中略)と太字強調は筆者による。
1 | //冗長なdelete |
2 | if (ptr) |
3 | { |
4 | delete ptr; |
5 | } |
6 |
7 | //楽ちんなdelete |
8 | delete ptr; |
VC++のoperator deleteの中身を見てみたら、内部でnullチェックを行っているようだ。
0 件のコメント:
コメントを投稿