c++ - C++ 标准到底在哪里说取消引用未初始化的指针是未定义的行为?

标签 c++ standards undefined-behavior language-lawyer

到目前为止,我找不到如何推断出以下内容:

int* ptr;
*ptr = 0;

是未定义的行为。

首先,5.3.1/1 指出 * 表示将 T* 转换为 T 的间接寻址。但这并没有说明任何关于 UB 的事情。

然后经常引用 3.7.3.2/4 说在非空指针上使用释放函数会使指针无效,以后使用无效指针是 UB。但是在上面的代码中没有任何关于释放的内容。

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

最佳答案

第 4.1 节看起来像一个候选(强调我的):

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”就能找到更多候选人。

关于c++ - C++ 标准到底在哪里说取消引用未初始化的指针是未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285895/

相关文章:

c - 使用 malloc(0) 和 memcpy

c++ - 一些未定义的行为是否比其他行为更未定义?

c++ - 多播大数据包包含到多个客户端的所有信息与到目标客户端的单个数据包

c++ - Boost 程序选项允许输入值集

c - 为什么标准托管 C 中的 "file"概念如此通用?

c++ - 为什么成员类型需要前向声明,而成员函数不需要?

c++ - i +=++i 在 C++0x 中是未定义的行为吗?

c++ - 红黑树插入,我想我可能把旋转弄乱了

c++ - x86暂停指令的跨平台实现

c - C 编译器编译 i = i++; 是否合法?作为系统("rm -rf/");?