为什么不把delete指针设置为NULL? [英] Why doesn't delete set the pointer to NULL?

查看:102
本文介绍了为什么不把delete指针设置为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是想知道为什么在删除之后将指针自动设置为NULL不是标准的一部分。如果这得到照顾,那么由于无效指针而导致的许多崩溃将不会发生。但是,我已经说过,我可以想到几个原因,为什么标准会限制这一点:

I always wondered why automatic setting of the pointer to NULL after delete is not part of the standard. If this gets taken care of then many of the crashes due to an invalid pointer would not occur. But having said that I can think of couple of reasons why the standard would have restricted this:



  1. 性能:

  1. Performance:

一个附加的指令可能会降低 delete 性能。

An additional instruction could slow down the delete performance.

这可能是因为 const 指针。

然后再次标准可以为这个特殊情况做一些事情我猜。

Then again standard could have done something for this special case I guess.


有没有人知道不允许这样做的确切原因?

Does anyone know exact reasons for not allowing this?

推荐答案

Stroustrup自己答案。摘录:


C ++显式允许一个
执行delete以清除
一个左值操作数,希望
的实现会这样做,
但是这个想法似乎没有
变得流行的实现者。

C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers.

但是他提出的主要问题是delete的参数不需要是一个左值。

But the main issue he raises is that delete's argument need not be an lvalue.

这篇关于为什么不把delete指针设置为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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