简体   繁体   English

删除 NULL 指针是否安全?

[英]Is it safe to delete a NULL pointer?

Is it safe to delete a NULL pointer?删除 NULL 指针是否安全?

And is it a good coding style?它是一种好的编码风格吗?

delete performs the check anyway, so checking it on your side adds overhead and looks uglier. delete无论如何都会执行检查,因此在您身边检查它会增加开销并且看起来更丑陋。 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我也很喜欢默认情况下delete将参数设置为 NULL 就像在

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

(I know about R and L values, but wouldn't it be nice?) (我知道 R 和 L 值,但这不是很好吗?)

From the C++0x draft Standard.来自 C++0x 标准草案。

$5.3.5/2 - "[...]In either alternative, the value of the operand of delete may be a null pointer value.[...'" $5.3.5/2 - “[...] 在任一选择中,删除操作数的值可能是空指针值。[...'”

Of course, no one would ever do 'delete' of a pointer with NULL value, but it is safe to do.当然,没有人会“删除”具有 NULL 值的指针,但这样做是安全的。 Ideally one should not have code that does deletion of a NULL pointer.理想情况下,不应有删除 NULL 指针的代码。 But it is sometimes useful when deletion of pointers (eg 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.由于删除 NULL 指针值是安全的,因此可以真正编写删除逻辑,而无需显式检查要删除的 NULL 操作数。

As an aside, C Standard $7.20.3.2 also says that 'free' on a NULL pointer does no action.顺便说一句,C 标准 $7.20.3.2 还说 NULL 指针上的“免费”没有任何作用。

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. free 函数导致 ptr 指向的空间被释放,即可供进一步分配。 If ptr is a null pointer, no action occurs.如果 ptr 是空指针,则不会发生任何操作。

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:由于上一句引起了混乱,因此举一个例子——这不是异常安全——描述的内容:

void somefunc(void)
{
    SomeType *pst = 0;
    AnotherType *pat = 0;

    …
    pst = new SomeType;
    …
    if (…)
    {
        pat = new AnotherType[10];
        …
    }
    if (…)
    {
        …code using pat sometimes…
    }

    delete[] pat;
    delete pst;
}

There are all sorts of nits that can be picked with the sample code, but the concept is (I hope) clear.可以使用示例代码选择各种各样的 nits,但这个概念(我希望)很清楚。 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;指针变量被初始化为零,这样函数末尾的delete操作就不需要在源代码中测试它们是否为非空; 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.如果您正在寻找良好的编码实践,请考虑使用智能指针,这样您就根本不需要delete

为了补充ruslik 的答案,在 C++14 中你可以使用这个结构:

delete std::exchange(heapObject, nullptr);

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.有一个关于这个问题的 常见问题解答可以回答这个问题。

The C++ language guarantees that delete p will do nothing if p is null. C++ 语言保证如果 p 为空,则 delete p 什么也不做。 Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.由于您可能会反向进行测试,并且由于大多数测试方法都强制您明确测试每个分支点,因此您不应该放入多余的 if 测试。

I have experienced that it is not safe (VS2010) to delete[] NULL (ie array syntax).我曾经历过,这不是安全的(VS2010)删除[] NULL(即数组语法)。 I'm not sure whether this is according to the C++ standard.我不确定这是否符合 C++ 标准。

It is safe to delete NULL (scalar syntax).删除 NULL(标量语法)安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM