解除分配位置的指针是否是未定义的行为? [英] Pointer to deallocated location Is it a Undefined Behavior?

查看:135
本文介绍了解除分配位置的指针是否是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解除分配位置的指针是否是未定义的行为?

Pointer to deallocated location Is it a Undefined Behavior?

http ://ideone.com/Qp3uY

int *p = new int;

*p = 10;

delete p;

*p = 10;

cout << *p << endl;


推荐答案

只有一个指向解除分配位置的指针不是未定义的行为本身。尝试取消引用该指针确实会产生未定义的行为。

There mere existence of a pointer to a deallocated location is not undefined behavior in itself. Attempting to dereference that pointer does produce undefined behavior though.

这篇关于解除分配位置的指针是否是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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