结构相同的共享首批成员的工会 [英] union of structs sharing same first members

查看:98
本文介绍了结构相同的共享首批成员的工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到pre-C11 C.比方说,实现结构多态的非传统的方式,我们有2个结构:

I have been looking into an un-traditional way of achieving struct "polymorphism" in pre-C11 C. Let's say we have 2 structs:

struct s1 {
    int var1;
    char var2;
    long var3;
};

struct s2 {
    int var1;
    char var2;
    long var3;
    char var4;
    int var5;
};

在大多数编译器,我们可以安全地指针之间转换为2个,然后访问常用的首批成员,如果没有填充发生。然而,这不是standartized行为。

On most compilers, we could safely cast between pointers to the two and then access the common first members if no padding takes place. However, this is not standartized behaviour.

现在,我发现在C标准以下行尽可能C89:

Now, I found the following line in the C standard as far as C89:

一个特别保证是为了简化使用工会制成:如果联合包含共享公共初始序列几种结构,如果联合对象当前包含这些结构中的一个,它允许检查公共初始其中的任何部分。两种结构有着共同的初始序列,如果相应成员具有兼容的类型的一个或多个初始成员的序列

One special guarantee is made in order to simplify the use of unions: If a union contains several structures that share a common initial sequence, and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them. Two structures share a common initial sequence if corresponding members have compatible types for a sequence of one or more initial members.

还规定以下内容:

一个指针联合对象,适当地投,分给每个成员(或者如果成员是一个位域,然后在它所在的单元),反之亦然。

A pointer to a union object, suitably cast, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

现在,如果我创建这两个结构的联合:

Now, if I create a union of these two structs:

union s2_polymorphic {
    struct s1 base;
    struct s2 derived;
};

和使用这种方式:

union s2_polymorphic test_s2_polymorphic, *ptest_s2_polymorphic;
struct s2 *ptest_s2;
struct s1 *ptest_s1;

ptest_s2_polymorphic = &test_s2_polymorphic;

ptest_s2 = (struct s2*)ptest_s2_polymorphic;

ptest_s2->var1 = 1;
ptest_s2->var2 = '2';

ptest_s1 = (struct s1*)ptest_s2;

printf("ptest_s1->var1 = %d\n", ptest_s1->var1);
printf("ptest_s1->var2 = %c\n", ptest_s1->var2);

其中编译和运行良好,并给出,在海湾合作委员会(GCC)4.8.3 20140911 ,输出

ptest_s1->var1 = 1                                                            
ptest_s1->var2 = 2

可否的行为被定义良好的,根据从标准以上给出的报价?

Will the behaviour be well-defined, according to the quotes from the standard given above?

推荐答案

经过一番研究,我觉得我有这个问题,一份合格的答卷。

After some research, I think I have a qualified answer for this question.

给出的引文是从C89标准。 C99和C11有它改写这样的:

The citation given was from the C89 standard. C99 and C11 have it rephrased like this:

一个特别保证是为了简化使用工会制成:如果联合包含共享一个共同的初始序列几种结构(见下文),并且如果联合对象当前包含这些结构中的一个,它允许检查任何他们的任何地方完成型工会的声明是可见的。

One special guarantee is made in order to simplify the use of unions: 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.

最后一部分可除preTED,恕我直言,以各种不同的方式。然而,commitee离开它,因为它是。据他们说,这意味着结构的共同起始部分的检查可以做到的只有的使用工会的对象键入已宣布遏制他们。即在<一个很好的证明href=\"http://stackoverflow.com/questions/21175830/exception-to-strict-aliasing-rule-in-c-from-6-5-2-3-structure-and-union-members\">this问题。

The last part can be interpreted, IMHO, in a variety of ways. However, the commitee left it as it is. According to them, it means that the inspection of the "common initial part" of the structures can be done only using an object of the union type that has been declared to contain them. That is very well demonstrated in this question.

怎么样C89,这似乎让我试图做什么?那么,在一个C89的编译器上,是的,这应该工作。但是,它可能不是真正需要:我不知道,支持严格别名单严格-C89的编译器上,因此,与他们,很容易简单地与公共初始序列投的结构对方的类型尽量不要给他们不同的包装设置。结果应该是相同的。

What about C89, which seems to allow what I was trying to do? Well, in a C89-conforming compiler, yes, this should work. However, it might not really be needed: I don't know of a single strictly-C89-conforming compiler that supports strict aliasing, so, with them, it is easier to simply cast the structs with the common initial sequence to each other's types and try not to give them different packing settings. The result should be the same.

这篇关于结构相同的共享首批成员的工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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