联合或结构是否允许从未初始化的实例进行分配? [英] Do a union or struct permit assignment from an uninitialised instance?

查看:99
本文介绍了联合或结构是否允许从未初始化的实例进行分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于将未初始化的自动变量分配给另一个相同类型的变量的定义性.

This question is about the definedness or otherwise of assigning an uninitalised automatic variable to another one of the same type.

考虑

typedef struct
{
    int s1;
    int s2;
} Foo;

typedef union
{
    int u1;
    Foo u2; 
} Bar;

int main()
{
    {
        int a;
        int b = a; // (1)
    }
    {
        Foo a;
        Foo b = a; // (2)
    }
    {   
        Bar a;
        a.u1 = 0;
        Bar b = a; // (3)
    }
}

请参考main中的注释:

(1).我知道的那么多.

(1) is undefined since a is uninitialised. That much I know.

但是(2)呢?结构成员s1s2尚未初始化.

But what about (2)? The struct members s1 and s2 are uninitialised.

此外,(3)怎么样?内存u2.s2未初始化,因此读取它是未定义行为,不是吗?

Furthermore, what about (3)? The memory u2.s2 is uninitialised, so reading it is undefined behaviour no?

推荐答案

在(1)和(2)中未定义行为.

The behavior is undefined in (1) and (2).

根据C标准,具有自动存储期限的未初始化对象的值不确定(C 2011 [N1570] 6.7.9 10).从名义上讲,这意味着它具有一定的价值,但是我们在编写程序时不知道它是什么.

Per the C standard, the value of an object with automatic storage duration that is not initialized is indeterminate (C 2011 [N1570] 6.7.9 10). Nominally, this means it has some value, but we do not know what it is while writing the program.

但是,该标准还说:如果左值指定了可以使用 register 存储类声明的自动存储持续时间的对象(从未使用其地址),并且该对象未初始化(未使用初始化程序声明并且在使用前未对其进行任何赋值),该行为是未定义的"(6.3.2.1 2).在您的示例代码中,a的地址永远不会被使用,也不会被初始化,并且在表达式中使用它是一个左值.因此,行为是不确定的.

However, the standard also says "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" (6.3.2.1 2). In your sample code, the address of a is never taken, and it is not initialized, and using it in an expression is an lvalue. Therefore, the behavior is undefined.

(此段落6.3.2.1 2旨在容纳可以检测未初始化寄存器使用的处理器.尽管如此,C标准中的规则适用于所有实现.)

(This passage, 6.3.2.1 2, was designed to accommodate processors that can detect use of an uninitialized register. Nonetheless, the rule in the C standard applies to all implementations.)

(3)并未由C标准明确解决.尽管已为联合的一个成员分配了一个值,因此对于6.3.2.1 2而言未进行初始化,但b = a中使用的对象是联合,而不是其成员.显然,我们的直觉概念是,如果为工会的成员分配了一个值,则该工会就有一个值.但是,我没有在C标准中看到这一点.

(3) is not clearly addressed by the C standard. Although a member of the union has been assigned a value, and hence is not uninitialized for purposes of 6.3.2.1 2, the object being used in b = a is the union, not its member. Obviously, our intuitive notion is that, if a member of a union is assigned a value, the union has a value. However, I do not see this specified in the C standard.

我们可以推断出6.3.2.1 2并不是要考虑将未初始化的联合或结构,至少是在其一部分已分配值的情况下,因为:

We can infer 6.3.2.1 2 is not intended to consider a union or structure to be uninitialized, at least if part of it has been assigned a value, because:

  1. 结构可以具有未命名的成员,例如未命名的位字段.

  1. Structures can have unnamed members, such as unnamed bit fields.

根据C 6.7.9 9,未命名的结构成员具有不确定的值,即使在(结构的)初始化之后也是如此.

Per C 6.7.9 9, unnamed members of structures have indeterminate value, even after initialization (of the structures).

如果6.3.2.1 2适用于未为每个成员分配值的结构,则如果a是具有未命名成员的结构并具有自动存储期限,则b = a将始终是未定义的.

If 6.3.2.1 2 applied to structures in which not every member had been assigned a value, then b = a would always be undefined if a were a structure with an unnamed member and had automatic storage duration.

这似乎是不合理的,而不是标准的意图.

That seems unreasonable and not what the standard intended.

但是,这里有一些摆动的空间.该标准可以指定仅在初始化结构或为其所有所有成员分配了值的情况下,才可以对结构进行未初始化.在这种情况下,如果a是仅向一个成员分配值的结构,则将不确定(3).我不认为这个摆动的房间有工会存在.如果已为工会成员分配了值,则仅考虑将工会未初始化是合理的.

However, there is some wiggle room here. The standard could have specified that a structure is not uninitialized only if it were initialized or all of its named members have been assigned values. In that case (3) would be undefined if a were a structure in which only one member had been assigned a value. I do not think this wiggle room exists with a union; if a member of the union has been assigned a value, it is only reasonable to consider the union not to be uninitialized.

这篇关于联合或结构是否允许从未初始化的实例进行分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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