在C ++中`delete`之后,指针是否保证保留其值? [英] Is the pointer guaranteed to preserve its value after `delete` in C++?

查看:228
本文介绍了在C ++中`delete`之后,指针是否保证保留其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题启发。

假设在C ++中代码我有一个有效的指针,并正确地 delete 它。根据C ++标准,指针将变得无效(3.7.3.2/4 - 解除分配函数将使所有指向解除分配存储的所有部分的指针无效)。

Suppose in C++ code I have a valid pointer and properly delete it. According to C++ standard, the pointer will become invalid (3.7.3.2/4 - the deallocation function will render invalid all pointers referring to all parts of deallocated storage).

至少在大多数实现中,它保存的值和将存储与 delete 之前完全相同的地址,但使用值是未定义的行为

At least in most implementations it preserves the value and will store exactly the same address as before delete, however using the value is undefined behavior.

标准保证指针将保留其值,值是否允许改变?

Does the standard guarantee that the pointer will preserve its value or is the value allowed to change?

推荐答案

不,不能保证,实现可能会合法地赋值为 em>操作符到 delete

No, it's not guaranteed and an implementation may legitimately assign zero to an lvalue operand to delete.

Bjarne Stroustrup希望实现会选择这样做, 。

Bjarne Stroustrup had hoped that implementations would choose to do this, but not many do.

http:// www .stroustrup.com / bs_faq2.html#delete-zero

这篇关于在C ++中`delete`之后,指针是否保证保留其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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