是“delete p; p = NULL(nullptr);“反模式? [英] Is "delete p; p = NULL(nullptr);" an antipattern?

查看:905
本文介绍了是“delete p; p = NULL(nullptr);“反模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索结果中,我偶然发现了这个问题并且其中一个评论到最被投票的答案(对最被投票的答案的第五个评论)表明 delete p; p = NULL; 是一个反模式。我必须承认,我碰巧使用它经常匹配它有时在大多数时候使用检查 if(NULL!= p) The Man 自己似乎建议它(请参阅 destroy()函数示例),所以我真的很困惑,为什么它可能是一个可怕的事情被视为一个反模式。我使用它的原因如下:




  • 当我释放一个资源时,我也想使它无效,是指向无效的正确工具

  • 我不想留下悬空指针

  • 我想避免double\multiple



请注意,如果没有指针,我不是在this指针的上下文中问这个问题,让我们假设我们不住在一个完美的C ++世界中,并且遗留代码确实存在并且必须被维护,所以请不要建议任何类型的智能指针:)

解决方案

是的,我不建议这样做。



原因是额外的设置为null将只在非常有限的上下文中有用。如果你在一个析构函数中,指针本身就不会在析构函数执行之后存在,这意味着它是否为null并不重要。



如果指针被复制,将 p 设置为null将不会设置其余的指针,然后你可以碰到同样的问题,你会期望找到被删除的指针是空的,并且它不会有意义你的指针变成非零,但对象不在那里....



此外,它可能会隐藏其他错误,例如,如果您的应用程序尝试多次删除指针,通过将指针设置为null,效果是第二次删除将转换为无操作,并且应用程序不会崩溃逻辑中的错误(考虑到稍后的编辑在 delete 之前访问指针,这不是失败的... 怎么会失败? p>

Searching something on SO, I stumbled across this question and one of the comments to the most voted answer (the fifth comment to that most voted answer) suggests that delete p; p = NULL; is an antipattern. I must confess that I happen to use it quite often paring it sometimes\most of the times with the check if (NULL != p). The Man himself seems to suggest it (please see the destroy() function example) so I'm really confused to why it might be such a dreaded thing to be considered an antipattern. I use it for the following reasons:

  • when I'm releasing a resource I also want to kind of invalidate it for further usage and NULL is the right tool to say a pointer is invalid
  • I don't want to leave behind dangling pointers
  • I want to avoid double\multiple free bugs - deleting a NULL pointer is like a nop but deleting a dangling pointer is like "shooting yourself in the foot"

Please note that I'm not asking the question in the context of the "this" pointer and let's assume we don't live in a perfect C++ world and that legacy code does exist and it has to be maintained, so please do not suggest any kind of smart pointer :).

解决方案

Yes, I would not recommended doing it.

The reason is that the extra setting to null will only help in very limited contexts. If you are in a destructor, the pointer itself will not exist right after the destructor execution, which means that whether it is null or not does not matter.

If the pointer was copied, setting p to null will not set the rest of the pointers, and you can then hit the same problems with the extra issue that you will be expecting to find deleted pointers being null, and it won't make sense how your pointer became non-zero and yet the object is not there....

Additionally it might hide other errors, like for example if your application is trying to delete a pointer many times, by setting the pointer to null, the effect is that the second delete will be converted to a no-op, and while the application will not crash the error in the logic is still there (consider that a later edit accesses the pointer right before the delete that is not failing... how can that fail?

这篇关于是“delete p; p = NULL(nullptr);“反模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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