C ++标准在什么地方说dereferencing未初始化的指针是未定义的行为? [英] Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?

查看:130
本文介绍了C ++标准在什么地方说dereferencing未初始化的指针是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我找不到如下推论:

So far I can't find how to deduce that the following:

int* ptr;
*ptr = 0;

是未定义的行为。

所有,有5.3.1 / 1表示 * 意指间接,将 T * 转换为 T

First of all, there's 5.3.1/1 that states that * means indirection which converts T* to T. But this doesn't say anything about UB.

然后经常引用3.7.3.2/4说,对非空指针使用解除分配函数会使指针无效,并且后来对无效指针的使用是UB。

Then there's often quoted 3.7.3.2/4 saying that using deallocation function on a non-null pointer renders the pointer invalid and later usage of the invalid pointer is UB. But in the code above there's nothing about deallocation.

如何在上面的代码中推导出UB?

How can UB be deduced in the code above?

推荐答案

第4.1节看起来像候选人(强调我):

Section 4.1 looks like a candidate (emphasis mine):



非函数非数组类型T的左值(3.10)可以被
转换为右值。如果T是一个
不完整类型,一个程序,
需要这个转换是
不成形。如果
lvalue引用的对象不是类型为
T的对象,并且不是从T派生的类型
的对象,或者如果对象是$ b $则为 b未初始化
必须进行此转换的程序
未定义的行为
。如果T是
非类类型,则右值的类型
是T的cv非限定版本。
否则,右值的类型是
T

An lvalue (3.10) of a non-function, non-array type T can be converted to an rvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the lvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the rvalue is the cv-unqualified version of T. Otherwise, the type of the rvalue is T.

我相信只要搜寻「uninitial」就能找到更多候选人。

I'm sure just searching on "uninitial" in the spec can find you more candidates.

这篇关于C ++标准在什么地方说dereferencing未初始化的指针是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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