使用不确定的值还是使用自动存储对象中存储的那些不确定值? [英] Is using any indeterminate value undefined or just those stored in objects with automatic storage?

查看:132
本文介绍了使用不确定的值还是使用自动存储对象中存储的那些不确定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C99 J.2,在以下情况下行为未定义:

According to C99 J.2, the behavior is undefined when:


在以下情况下使用具有自动存储持续时间的对象的值:它是
不确定的

The value of an object with automatic storage duration is used while it is indeterminate

在其他所有情况下,对象具有不确定的值怎么办?如果使用它们,是否还会始终调用UB?还是仅在它们包含陷阱表示形式时才调用UB?

What about all the other cases where an object has an indeterminate value? Do we also always invoke UB if we use them? Or do we invoke UB only when they contain a trap representation?

示例包括:


  • 使用 malloc (7.20.3.3p2)

  • [存储在非自动存储中]分配的对象的值在调用 fclose 后调用 FILE * (7.19.3p4)

  • [存储在非自动存储中的指针(6.2.4p2)调用了 free

  • the value of an object allocated using malloc (7.20.3.3p2)
  • [storing in non-automatic storage] a FILE* after calling fclose on it (7.19.3p4)
  • [storing in non-automatic storage] a pointer after calling free on it (6.2.4p2)

...以此类推。

我使用C99作为参考,但随意在您的答案中引用C99或C11。

I've used C99 for my references, but feel free to refer to C99 or C11 in your answer.

推荐答案

我在这里使用C11修订版:

I am using C11 revision here:

该标准的定义为:


不确定值

未指定值或陷阱表示形式

either an unspecified value or a trap representation

陷阱表示形式

不需要表示对象类型值的对象表示形式

an object representation that need not represent a value of the object type

未指定值

相关类型的未指定有效值,其中本国际标准不对选择值的
施加任何要求在任何情况下

Unspecified valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance




未指定的值是相关类型的有效值,因此不会引起未定义的行为。


An unspecified value is a valid value of the relevant type and as such it does not cause undefined behaviour. Using a trap representation will.

但是在标准中使用此措辞的原因是摘录使编译器能够发出诊断信息,或拒绝使用未初始化的局部变量的值的em>程序仍然符合标准;因为据说其中有些类型不能在内存中包含陷阱表示,所以它们始终处于不确定状态的未指定值。例如,这适用于 unsigned char 。而且,由于使用未指定的值没有未定义的行为,因此该标准不允许一个人拒绝这样的程序。

But why this wording exists in the standard is that the excerpt enables compilers to issue diagnostics, or reject programs that use the value of uninitialized local variables yet still stay standard-compliant; because there are types of which it is said that they cannot contain trap representations in memory, so they'd always be having unspecified value there in their indeterminate state. This applies to for example unsigned char. And since using an unspecified value does not have undefined behaviour then the standard does not allow one to reject such a program.

另外,说一个 unsigned char 通常不具有陷阱表示...,除了IIRC之外,计算机体系结构中的寄存器可以设置为未初始化,并且从这种体系结构中的寄存器读取将触发故障。因此,即使 unsigned char 在内存中实际上没有陷阱表示,在这种体系结构上,如果它具有自动存储持续时间,也将有100%的概率引起硬件故障。然后编译器决定将其存储在寄存器中,并且在调用时仍未初始化。

Additionally, say an unsigned char normally does not have a trap representation... except, IIRC there are computer architectures where a register can be set to "uninitialized", and reading from a register in such an architecture will trigger a fault. Thus even if an unsigned char does not really have trap representations in memory, on this architecture it will with cause a hardware fault with 100 % probability, if it is of automatic storage duration and compiler decides to store it in a register and it is still uninitialized at the time of the call.

这篇关于使用不确定的值还是使用自动存储对象中存储的那些不确定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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