且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-27 04:57:36

这是未定义的行为.

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

According to the standard ([dcl.init] paragraph 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 的访问,这可能导致不确定的值而不是未定义的值.

with all the following cases addressing access of unsigned narrow character type or std::byte, which can result in an indeterminate value instead of being undefined.