且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除NULL指针是否安全?

更新时间:2023-12-01 08:07:52

delete 执行检查,所以检查它在你的一面增加了开销,看起来更丑陋。 非常的良好做法是在 delete (有助于避免双重删除和其他类似的内存损坏问题)后将指针设置为NULL。

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).

如果 delete 默认情况下将参数设置为NULL,我也会喜欢

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

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

(我知道R和L的值,但不会很好?)

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