c – 删除的指针指向什么?

前端之家收集整理的这篇文章主要介绍了c – 删除的指针指向什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
int*a=nullptr; //NULL before C++11
a=new int(1);
delete a;

现在有什么意义?它是指向nullptr还是它指向它被删除之前指向的地址?

解决方法

根据C标准(6.7存储时间)

4 When the end of the duration of a region of storage is reached,the
values of all pointers representing the address of any part of that
region of storage become invalid pointer values (6.9.2).
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.

所以在这个表达式声明后

delete a;

指针a的值未更改但已变为无效.这个地址都不存在任何对象.

猜你在找的C&C++相关文章