C++ 删除 NULL 指针是否安全?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4190703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:46:55  来源:igfitidea点击:

Is it safe to delete a NULL pointer?

c++pointersmemory-managementnull-pointerdelete-operator

提问by qiuxiafei

Is it safe to delete a NULL pointer?

删除 NULL 指针是否安全?

And is it a good coding style?

它是一种好的编码风格吗?

采纳答案by ruslik

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

delete无论如何都会执行检查,因此在您身边检查它会增加开销并且看起来更丑陋。一个非常好的做法后,指针设置为NULL delete(有助于避免双重缺失和其它类似的内存损坏问题)。

I'd also love if deleteby 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 值,但这不是很好吗?)

回答by Chubsdad

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

当然,没有人会“删除”具有 NULL 值的指针,但这样做是安全的。理想情况下,不应有删除 NULL 指针的代码。但有时在循环中删除指针(例如在容器中)时它很有用。由于删除 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. If ptr is a null pointer, no action occurs.

free 函数导致 ptr 指向的空间被释放,即可供进一步分配。如果 ptr 是空指针,则不会发生任何操作。

回答by Jonathan Leffler

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. The pointer variables are initialized to zero so that the deleteoperations 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.

可以使用示例代码选择各种各样的 nits,但这个概念(我希望)很清楚。指针变量被初始化为零,这样delete函数末尾的操作就不需要在源代码中测试它们是否为非空;无论如何,库代码都会执行该检查。

回答by Brian R. Bondy

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 deleteat all.

如果您正在寻找良好的编码实践,请考虑使用智能指针,这样您就完全不需要了delete

回答by ashrasmun

To complement ruslik's answer, in C++14 you can use this construction:

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

delete std::exchange(heapObject, nullptr);

回答by ashrasmun

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.

除非您重载删除运算符,否则它是安全的。如果您重载了删除操作符并且不处理空条件,那么它根本不安全。

回答by pheest

I have experienced that it is notsafe (VS2010) to delete[] NULL (i.e. array syntax). I'm not sure whether this is according to the C++ standard.

我所经历的,这是不是安全的(VS2010)删除[] NULL(即数组语法)。我不确定这是否符合 C++ 标准。

It issafe to delete NULL (scalar syntax).

安全的删除NULL(标量语法)。