Is it safe to delete a NULL pointer?

前端 未结 7 1505
忘掉有多难
忘掉有多难 2020-11-22 06:51

Is it safe to delete a NULL pointer?

And is it a good coding style?

7条回答
  •  Happy的楠姐
    2020-11-22 07:08

    delete performs the check anyway, so checking it on your side adds overhead and looks uglier. A very good practice is setting the pointer to NULL after delete (helps avoiding double deletion and other similar memory corruption problems).

    I'd also love if delete by default was setting the parameter to NULL like in

    #define my_delete(x) {delete x; x = NULL;}
    

    (I know about R and L values, but wouldn't it be nice?)

提交回复
热议问题