gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:47:06 25 4
gpt4 key购买 nike

例如,在下面的代码中:

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 的访问,这可能导致不确定值而不是未定义。

关于c++ - 在 C++ 中,访问未初始化的数组是未指定行为还是未定义行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49696810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com