是^ A或A-一个未定义的行为,如果没有初始化? [英] Is a^a or a-a undefined behaviour if a is not initialized?

查看:113
本文介绍了是^ A或A-一个未定义的行为,如果没有初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此程序:

#include <stdio.h>

int main(void)
{
    unsigned int a;
    printf("%u %u\n", a^a, a-a);
    return 0;
}

它是不确定的行为?

Is it undefined behaviour?

在面对它, A 是一个未初始化的变量。所以这点不确定的行为。但 A ^ A AA 等于 0 的的所有值 A ,至少我认为是这样的。是否有可能,有一些方法来认为,行为是明确界定?

On the face of it, a is an uninitialized variable. So that points to undefined behaviour. But a^a and a-a are equal to 0 for all values of a, at least I think that is the case. Is it possible that there is some way to argue that the behaviour is well defined?

推荐答案

在C11:


  • 它是根据6.3.2.1/2明确未定义如果 A 从未有采取(以下引用)地址

  • 这可能是一个陷阱重新presentation(这将导致UB访问时)。 6.2.6.1/5:

  • It's explicitly undefined according to 6.3.2.1/2 if a never has its address taken (quoted below)
  • It could be a trap representation (which causes UB when accessed). 6.2.6.1/5:

某些对象重新presentations不必重新present对象类型的值。

Certain object representations need not represent a value of the object type.

无符号整型可以有陷阱重新presentations(例如,如果它有15 precision位和1个奇偶校验位,访问 A 可能导致奇偶校验错误)

Unsigned ints can have trap representations (e.g. if it has 15 precision bits and 1 parity bit, accessing a could cause a parity fault).

6.2.4 / 6说,初始值是<青霉>不确定的和那3.19.2下的定义是<青霉>任一未指定的值,或一个陷阱重新presentation

6.2.4/6 says that the initial value is indeterminate and the definition of that under 3.19.2 is either an unspecified value or a trap representation.

另外:在C11 6.3.2.1/2,由帕斯卡尔Cuoq指出:

Further: in C11 6.3.2.1/2, as pointed out by Pascal Cuoq:

如果左值指定自动存储持续时间的对象,它本来是
  使用寄存器存储类中声明(从来没有考虑它的地址),而该对象
  未初始化(不是一个初始化声明并没有分配给它一直
  使用前进行)的行为是不确定的。

If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

这不具有字符类型的异常,因此这一条款似乎取代了preceding讨论;访问 X 是即使没有陷阱再presentations存在不确定立即。这一条款加入C11 支持安腾处理器里面做实际上有一个陷阱状态寄存器。

This doesn't have the exception for character types, so this clause appears to supersede the preceding discussion; accessing x is immediately undefined even if no trap representations exist. This clause was added to C11 to support Itanium CPUs which do actually have a trap state for registers.

没有陷阱再presentations系统:但是,如果我们扔在&放大器; X; 这样的6.3.2.1/2's反对意见不再适用,我们都是已知有没有陷阱再presentations的系统上?然后该值是的未经指定的值的。
中的未经指定的值的在3.19.3是有点模糊,但它是通过的DR 451 ,该结论是:

Systems without trap representations: But what if we throw in &x; so that that 6.3.2.1/2's objection no longer applies, and we are on a system that is known to have no trap representations? Then the value is an unspecified value. The definition of unspecified value in 3.19.3 is a bit vague, however it is clarified by DR 451, which concludes:


  • 描述可以出现来改变其值的条件下,未初始化值。

  • 在不定值执行任何操作将产生不确定的值作为结果。

  • 在不确定的值时使用
  • 库功能将表现出不确定的行为。

  • 这些答案适用于那些没有陷阱再presentations所有类型。

根据这项决议, int类型的; &放大器;一个; INT B = A - A; 的结果 B 仍然有不确定的值

Under this resolution, int a; &a; int b = a - a; results in b having indeterminate value still.

请注意,如果不确定的值不会传递给库函数,我们仍处于不确定的行为(而不是不确定的行为)的境界。结果可能是怪异,例如(!当J = J)如果富(); 可以调用foo,但是恶魔必须在鼻腔保持躲藏

Note that if the indeterminate value is not passed to a library function, we are still in the realm of unspecified behaviour (not undefined behaviour). The results may be weird, e.g. if ( j != j ) foo(); could call foo, but the demons must remain ensconced in the nasal cavity.

这篇关于是^ A或A-一个未定义的行为,如果没有初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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