在 C++ 中,访问未初始化的数组是未指定行为还是未定义行为?

In C++, is accessing an uninitialized array unspecified behavior or undefined behavior?

例如,在下面的代码中:

int myarray[3];
int x = myarray[1];

代码是否保证在常数时间内成功执行,x有一些整数值?或者编译器是否可以完全跳过为此发出代码/发出代码以启动 GNU Chess 并仍然符合 C++ 标准?

这在类似于数组的数据结构中很有用,但可以在恒定时间内初始化。 (抱歉,手边没有我的 Aho、Hopcroft 和 Ullman,所以无法查找名称。)

访问任何未初始化的数据是未定义的行为。

这是未定义的行为。

根据标准([dcl.init]第12段),

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced ... If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases

以下所有情况都解决了对无符号窄字符类型或 std::byte 的访问,这可能导致不确定值而不是未定义。