删除 NULL 指针安全吗?
删除 NULL 指针安全吗?
这是一种好的编码风格吗?
Is it safe to delete a NULL pointer?
And is it a good coding style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
删除 NULL 指针安全吗?
这是一种好的编码风格吗?
Is it safe to delete a NULL pointer?
And is it a good coding style?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
来自 C++0x 标准草案。
当然,没有人会“删除”具有 NULL 值的指针,但这样做是安全的。理想情况下,不应有删除 NULL 指针的代码。但事实是有时在循环中删除指针(例如在容器中)时很有用,因为删除 NULL 指针值是安全的,因此可以真正编写删除逻辑,而无需显式检查要删除的 NULL 操作数
。 .3.2 还指出 NULL 指针上的“free”不执行任何操作。
From the C++0x draft Standard.
Of course, no one would ever do 'delete' of a pointer with NULL value, but it is safe to do. Ideally one should not have code that does deletion of a NULL pointer. But it is sometimes useful when deletion of pointers (e.g. in a container) happens in a loop. Since delete of a NULL pointer value is safe, one can really write the deletion logic without explicit checks for NULL operand to delete.
As an aside, C Standard $7.20.3.2 also says that 'free' on a NULL pointer does no action.
是的,它很安全。
删除空指针没有什么坏处;如果未分配的指针被初始化为零然后简单地删除,它通常会减少函数尾部的测试数量。
由于前面的句子引起了混乱,所以用一个示例(不是异常安全的)来描述所描述的内容:
可以通过示例代码挑选出各种各样的问题,但概念(我希望)是清晰的。指针变量被初始化为零,这样函数末尾的删除操作就不需要在源代码中测试它们是否为非空;无论如何,库代码都会执行该检查。
Yes it is safe.
There's no harm in deleting a null pointer; it often reduces the number of tests at the tail of a function if the unallocated pointers are initialized to zero and then simply deleted.
Since the previous sentence has caused confusion, an example — which isn't exception safe — of what is being described:
There are all sorts of nits that can be picked with the sample code, but the concept is (I hope) clear. The pointer variables are initialized to zero so that the
delete
operations at the end of the function do not need to test whether they're non-null in the source code; the library code performs that check anyway.删除空指针没有任何效果。这不一定是好的编码风格,因为不需要它,但它也不错。
如果您正在寻找良好的编码实践,请考虑使用智能指针,这样您就根本不需要
删除
。Deleting a null pointer has no effect. It's not good coding style necessarily because it's not needed, but it's not bad either.
If you are searching for good coding practices consider using smart pointers instead so then you don't need to
delete
at all.为了补充 ruslik 的答案,在 C++14 中,您可以使用以下构造:
To complement ruslik's answer, in C++14 you can use this construction:
除非您重载了删除运算符,否则它是安全的。如果您重载了删除运算符并且不处理 null 条件,那么它根本不安全。
It is safe unless you overloaded the delete operator. if you overloaded the delete operator and not handling null condition then it is not safe at all.
关于此问题有一个常见问题解答来回答这个问题。
There is a FAQ on this matter which answers this question.
直接调用delete而不检查它是否为空是安全且更好的做法。
请参阅 https://clang.llvm.org/extra/clang-tidy/checks/readability/delete-null-pointer.html#:~:text=检查%20%20if%20语句%20其中,null%20指针%20有%20没有%20效果。
It is safe and better practice to directly call delete without checking if it is null.
see https://clang.llvm.org/extra/clang-tidy/checks/readability/delete-null-pointer.html#:~:text=Checks%20the%20if%20statements%20where,null%20pointer%20has%20no%20effect.
我的经验是删除[] NULL(即数组语法)不安全(VS2010)。
我不确定这是否符合 C++ 标准。
删除 NULL(标量语法)是安全的。
I have experienced that it is not safe (VS2010) to delete[] NULL (i.e. array syntax).
I'm not sure whether this is according to the C++ standard.
It is safe to delete NULL (scalar syntax).
delete
无论如何都会执行检查,因此在您这边检查会增加开销并且看起来更难看。一个非常好的做法是在删除
后将指针设置为NULL(有助于避免重复删除和其他类似的内存损坏问题)。我也希望
delete
默认情况下将参数设置为 NULL(我知道 R 和 L 值,但这不是很好吗?)
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 afterdelete
(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(I know about R and L values, but wouldn't it be nice?)