在删除之前是否有任何理由检查NULL指针? [英] Is there any reason to check for a NULL pointer before deleting?

查看:168
本文介绍了在删除之前是否有任何理由检查NULL指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在删除指针之前,我常常会看到 NULL 的旧代码检查,类似于

I often see legacy code checking for NULL before deleting a pointer, similar to,

if (NULL != pSomeObject) 
{
    delete pSomeObject;
    pSomeObject = NULL;
}

有任何理由检查 / code>指针之前删除它?

Is there any reason to checking for a NULL pointer before deleting it? What is the reason for setting the pointer to NULL afterwards?

推荐答案

是什么原因将指针设置为 NULL 删除空指针是完全安全的;它实际上相当于没有操作。

It's perfectly "safe" to delete a null pointer; it effectively amounts to a no-op.

在删除之前,您可能想要检查null的原因是试图删除一个空指针可能表示您的程序中的一个错误。

The reason you might want to check for null before you delete is that trying to delete a null pointer could indicate a bug in your program.

这篇关于在删除之前是否有任何理由检查NULL指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆