gpt4 book ai didi

c - 使用公共(public)初始序列初始化两个结构的并集

转载 作者:行者123 更新时间:2023-12-01 13:16:17 26 4
gpt4 key购买 nike

问题:如果 union 包含两个具有兼容类型的公共(public)初始序列的结构,那么如果我们使用一个结构初始化初始序列的某些部分并使用初始序列的其余部分,则行为定义明确使用另一个结构的序列?

考虑以下代码片段:

union u_t{
struct {
int i1;
int i2;
} s1;

struct {
int j1;
int j2;
} s2;
};

int main(){
union u_t *u_ptr = malloc(sizeof(*u_ptr));
u_ptr -> s1.i1 = 10;
u_ptr -> s2.j2 = 11;

printf("%d\n", u_ptr -> s2.j1 + u_ptr -> s1.i2); //prints 21
}

DEMO

问题是“打印 21”行为是否定义明确。标准 N1570 6.5.2.3(p6) 规定了以下内容:

if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible.

因此可以检查公共(public)初始序列(在本例中为整个结构)。但问题是,在这种情况下, union 似乎包含 s2 对象,其中 j2 是唯一的初始化成员。

我认为我们最终会出现未指定行为,因为我们只初始化了 s2.j2s2.j1 做了不是,所以它应该包含未指定的值。

最佳答案

关于别名:

公共(public)初始序列只关心两种结构类型的别名。这在这里不是问题,您的两个结构甚至是兼容的类型,因此指向它们的指针可能会在不使用任何技巧的情况下产生别名。剖析 C11 6.2.7:

6.2.7 Compatible type and composite type
Two types have compatible type if their types are the same. /--/ Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements:

If one is declared with a tag, the other shall be declared with the same tag.

这两个结构都没有在此处使用标记声明。

If both are completed anywhere within their respective translation units, then the following additional requirements apply:

它们都已完成(已定义)。

there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types;

这适用于这些结构。

if one member of the pair is declared with an alignment specifier, the other is declared with an equivalent alignment specifier; and if one member of the pair is declared with a name, the other is declared with the same name.

对齐说明符不适用。

For two structures, corresponding members shall be declared in the same order.

这是正确的。

结论是你的两个结构都是兼容的类型。这意味着您不需要像通用初始序列这样的任何技巧。严格的别名规则简单地说明 (6.5/7):

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
— a type compatible with the effective type of the object,

这里就是这种情况。

此外,如其他答案所述,此处实际数据的有效类型是int,因为分配的存储不会产生有效类型,因此它成为第一个用于左值访问的类型。这也意味着编译器不能假设指针不会别名。

此外,严格的别名规则为结构和 union 成员的左值访问提供了一个异常(exception):

an aggregate or union type that includes one of the aforementioned types among its members

然后你在上面有一个共同的初始序列。就别名而言,这是尽可能明确的定义。


关于类型双关语:

您真正关心的似乎不是别名,而是通过 union 输入双关语。 C11 6.5.2.3/3 模糊地保证了这一点:

A postfix expression followed by the . operator and an identifier designates a member of a structure or union object. The value is that of the named member,95) and is an lvalue if the first expression is an lvalue.

那是规范文本,写得很糟糕——没有人能理解程序/编译器应该如何基于此运行。内容丰富的脚注 95) 解释得很好:

95) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.

在您的情况下,您触发了从一种结构类型到另一种兼容结构类型的类型转换。这是非常安全的,因为它们是完全相同的类型,并且对齐或陷阱的问题不适用。

请注意这里的 C++ 是不同的。

关于c - 使用公共(public)初始序列初始化两个结构的并集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com