删除 NULL 指针安全吗?

发布于 2024-10-03 01:13:11 字数 42 浏览 6 评论 0原文

删除 NULL 指针安全吗?

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

Is it safe to delete a NULL pointer?

And is it a good coding style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

你对谁都笑 2024-10-10 01:13:12

来自 C++0x 标准草案。

$5.3.5/2 - “[...]无论是
另类,操作数的值
的delete可能是空指针
值。[...'"

当然,没有人会“删除”具有 NULL 值的指针,但这样做是安全的。理想情况下,不应有删除 NULL 指针的代码。但事实是有时在循环中删除指针(例如在容器中)时很有用,因为删除 NULL 指针值是安全的,因此可以真正编写删除逻辑,而无需显式检查要删除的 NULL 操作数

。 .3.2 还指出 NULL 指针上的“free”不执行任何操作。

free函数导致空间
ptr 指向要释放的,
也就是说,可用于进一步
分配。如果 ptr 是空指针,
没有任何操作发生。

From the C++0x draft Standard.

$5.3.5/2 - "[...]In either
alternative, the value of the operand
of delete may be a null pointer
value.[...'"

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.

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.

青衫负雪 2024-10-10 01:13:12

是的,它很安全。

删除空指针没有什么坏处;如果未分配的指针被初始化为零然后简单地删除,它通常会减少函数尾部的测试数量。


由于前面的句子引起了混乱,所以用一个示例(不是异常安全的)来描述所描述的内容:

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;
}

可以通过示例代码挑选出各种各样的问题,但概念(我希望)是清晰的。指针变量被初始化为零,这样函数末尾的删除操作就不需要在源代码中测试它们是否为非空;无论如何,库代码都会执行该检查。

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

殊姿 2024-10-10 01:13:12

删除空指针没有任何效果。这不一定是好的编码风格,因为不需要它,但它也不错。

如果您正在寻找良好的编码实践,请考虑使用智能指针,这样您就根本不需要删除

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.

关于从前 2024-10-10 01:13:12

为了补充 ruslik 的答案,在 C++14 中,您可以使用以下构造:

delete std::exchange(heapObject, nullptr);

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

delete std::exchange(heapObject, nullptr);
别再吹冷风 2024-10-10 01:13:12

除非您重载了删除运算符,否则它是安全的。如果您重载了删除运算符并且不处理 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.

时间海 2024-10-10 01:13:12

关于此问题有一个常见问题解答来回答这个问题。

C++ 语言保证,如果 p 是,则删除 p 将不会执行任何操作
无效的。因为你可能会向后测试,并且因为大多数测试
方法论迫使你明确地测试每个分支点,你
不应放入多余的 if 测试。

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

玉环 2024-10-10 01:13:12

我的经验是删除[] 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).

£噩梦荏苒 2024-10-10 01:13:11

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

我也希望 delete 默认情况下将参数设置为 NULL

#define my_delete(x) {delete x; x = 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 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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文