正在读取一个不确定的值未定义的行为? [英] Is reading an indeterminate value undefined behavior?

查看:128
本文介绍了正在读取一个不确定的值未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题出现在对问题的回答的评论中













$ b

有关代码在不初始化其值的情况下分配 bool 的(local)数组。

  const int n = 100; 
bool b [n];

显然, b 中的值不确定。



一些评论者认为, b [0] 是未定义的行为。这是在C ++标准的任何地方说明?我仍然相信相反:


  1. 有明确的存储分配和基本bool类型的初始化是完整的,因为它没有构造函数。因此,它肯定不同于取消引用未初始化的指针,或调用未初始化的非平凡对象上的方法/转换运算符。这些具体情况似乎包括在标准中。


  2. 行为确实未定义在C:在C中声明的未初始化变量会发生什么?它有价值吗?一些受访者似乎混淆了这两个。


  3. 在最新的C ++ 0x草稿中,我找不到不确定值的定义,特别是没有定义,值触发处理器陷阱。事实上,Bjarne Stroustrup不确定什么是贬值: http://zamanbakshifirst.blogspot。 com / 2007/02 / c-indeterminate-value.html



解决方案>

是的,正式的不确定值的右值转换是UB(除了 unsigned char ,最初我写的是和变体,但我记得正式迎合1补充有符号的字符,其中可能减去0可以用作陷阱值)



我太懒得为你做标准段落查找,也懒得关心对于



来说,可能是一个问题。实际上,只有一个问题存在于(1)古体系结构和(2)64位系统。



EDIT :oops,我现在似乎回想起一个博客帖子和关于正式UB的关联缺陷报告,用于访问不确定的char。所以也许我要实际检查标准,+搜索DR。

EDIT2 :Johannes Schaub非常善于提供 http://stackoverflow.com/questions/3892098/ctor-initializer-self-initialization-causes-crash/3892209#3892209\">链接到SO问题,其中讨论了访问char的UB。所以,这是我记得它的地方!谢谢,约翰尼斯。



欢呼& hth。,


The question arose in the comments of an answer to the question Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?

The code in question allocates a (local) array of bool without initializing their value.

const int n = 100;
bool b[n];

Clearly the values in b are indeterminate.

Some of the commenters opined that reading e.g. b[0] was undefined behavior. Is this stated anywhere in the C++ standard? I am still convinced of the opposite:

  1. There is clearly storage allocated and initialization of the fundamental bool type is complete, since it doesn't have a constructor. It is thus certainly not the same as dereferencing an uninitialized pointer, or calling methods/cast operators on uninitialized non-trivial objects. These specific cases seem to be covered by the standard.

  2. The behavior is indeed undefined in C: What happens to a declared, uninitialized variable in C? Does it have a value? and some respondents seem to confuse the two.

  3. In the latest C++0x draft I can find no definition of indeterminate value especially no definition that would allow accessing such a value to trigger a processor trap. Indeed, Bjarne Stroustrup is not sure what an inderminate value may be: http://zamanbakshifirst.blogspot.com/2007/02/c-indeterminate-value.html

解决方案

yes, formally an rvalue conversion of indeterminate value is UB (except for unsigned char, originally i wrote "and variants" but as i recall the formal caters to 1's complement signed char where possibly minus 0 could be used as trap value)

i'm too lazy to do the standard paragraph lookup for you, and also to lazy to care about downvotes for that

however, in practice only a problem on (1) archaic architectures, and perhaps (2) 64-bit systems.

EDIT: oops, i now seem to recall a blog posting and associated Defect Report about formal UB for accessing indeterminate char. so perhaps i'll have to actually check the standard, + search DRs. argh, it will have to be later then, now coffee!

EDIT2: Johannes Schaub was kind enough to provide this link to SO question where that UB for accessing char was discussed. So, that's where I remembered it from! Thanks, Johannes.

cheers & hth.,

这篇关于正在读取一个不确定的值未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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