删除后c ++中的指针 [英] Pointers in c++ after delete

查看:91
本文介绍了删除后c ++中的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了许多有关此的文章之后,我想澄清下一点:

After reading many posts about this, I want to clarify the next point:

A* a = new A();
A* b = a;

delete a;

A* c = a; //illegal - I know it (in c++ 11)
A* d = b; //I suppose it's legal, is it true?

所以问题是关于使用已删除指针的副本.

So the question is about using the value of copy of deleted pointer.

我已经读过,在c ++ 11中读取a的值会导致未定义的行为-但是读取b的值怎么办?

I've read, that in c++ 11 reading the value of a leads to undefined behaviour - but what about reading the value of b?

尝试读取指针的值(注意:这与 自C ++ 14起,将其取消引用)会导致实现定义的行为, 这可能包括生成运行时错误. (在C ++ 11中, 不确定的行为) 删除后指针本身会发生什么?

Trying to read the value of the pointer (note: this is different to dereferencing it) causes implementation-defined behaviour since C++14, which may include generating a runtime fault. (In C++11 it was undefined behaviour) What happens to the pointer itself after delete?

推荐答案

两者:

A* c = a;
A* d = b;

在C ++ 11中是未定义的,在C ++ 14中是定义的实现.这是因为ab都是无效的指针值"(因为它们指向已释放的存储空间),并且使用无效的指针值"是未定义的或实现的定义,具体取决于C ++版本. (使用"包括复制的值".)

are undefined in C++11 and implementation defined in C++14. This is because a and b are both "invalid pointer values" (as they point to deallocated storage space), and "using an invalid pointer value" is either undefined or implementation defined, depending on the C++ version. ("Using" includes "copying the value of").

C ++ 11中的相关部分([basic.stc.dynamic.deallocation]/4)读取(添加了重点):

The relevant section ([basic.stc.dynamic.deallocation]/4) in C++11 reads (emphasis added):

如果标准库中分配给释放函数的参数是不是空指针值(4.10)的指针,则释放函数应释放该指针引用的存储,从而使 all 无效>指向已释放存储的任何部分的指针.使用无效的指针值(包括将其传递给释放函数)的效果是不确定的.

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage. The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.

带有非规范性注释,说明:

with a non-normative note stating:

在某些实现中,它会导致系统生成的运行时

On some implementations, it causes a system-generated runtime

在C ++ 14中,相同的部分显示为:

In C++14 the same section reads:

如果标准库中分配给释放函数的参数是不是空指针值(4.10)的指针,则释放函数应释放该指针引用的存储,从而使 all 无效>指向已释放存储的任何部分的指针.通过无效的指针值进行间接并将无效的指针值传递给释放函数具有未定义的行为.任何其他使用无效指针值的行为都有实现定义的行为.

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage. Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior.

带有非规范性注释,说明:

with a non-normative note stating:

某些实现可能会定义复制无效的指针值会导致系统生成的运行时错误

Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault

这篇关于删除后c ++中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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