共同第一部件结构的联盟 [英] Union of structs with common first member

查看:106
本文介绍了共同第一部件结构的联盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定的code是否有在断言投指针别名(或其他符合标准的问题)。看来,一个指向联合类型应该可以被转换为第一个成员的指针,因为工会仅由这两种结构的,我觉得投来的第一个成员应该工作,但我不知道这是正确的,或者如果我粉饰填充细节的过程。需要垫工会高位?

I am unsure of whether or not the code has pointer aliasing (or other standard conformance issues) in the asserts cast. It seems that a pointer to the union type should be able to be cast to a pointer of the first member and since the union is only composed of these two structs, I think a cast to the first member should work, but I'm not sure if this is correct or if I'm glossing over padding details in the process. Are unions required to pad the upper bits?

看来,因为这是不确定的行为?没有人有任何见解,这是否是支载。我知道有利用结构与枚举类型字段和结构container_storage 这样做的另一种标准方法成员,但它似乎是浪费空间考虑到这一信息已经在结构包含

It seems as this is unspecified behavior? Does anyone have any insight as to whether this is suported. I know that there is an alternative standard way of doing this by using a struct with a enum type field and struct container_storage member, but it seems like a waste of space considering that this information is already in struct contained

在linux编译命令:的gcc -std = C99 -Wextra -pedantic -fstrict走样test.c的&放大器;&安培; ./a.out&功放;&安培;回声$?收益 0

compilation command in linux: gcc -std=c99 -Wextra -pedantic -fstrict-aliasing test.c && ./a.out && echo $? returns 0

#include <stdlib.h>
#include <assert.h>

enum type {type_a = 1, type_b = 2};

struct contained {
    int some_other_field;
    enum type type;
};

struct container_a {
    struct contained contained;
    int test;
};


struct container_b {
    struct contained contained;
    char test;
};

union container_storage {
    struct container_a container_a;
    struct container_b container_b;
};

int
main(int argc, char **argv)
{
    union container_storage a =
        {.container_a = {.contained = {.type = type_a}, .test = 42}};
    union container_storage b =
        {.container_b = {.contained = {.type = type_b}, .test = 'b'}};

    assert(((struct contained *)&a)->type == type_a);
    assert(((struct contained *)&b)->type == type_b);

    return EXIT_SUCCESS;
}

参考文献:

[1] <一个href=\"http://stackoverflow.com/questions/2906365/gcc-strict-aliasing-and-casting-through-a-union\">gcc,严格走样,并通过工会铸造

[2] 什么是严格别名规则?

推荐答案

这应该罚款。 C11,6.5.2.3/6(结构和工会会员)说:

That should be fine. C11, 6.5.2.3/6 ("Structure and union members") says:

一个特殊的保证是为了简化使用工会提出
  共享一个公共初始序列几种结构(见下文),并且如果结合
  对象目前包含这些结构中的一个,它允许检查共同
  其中的任何初始部分的任何地方已完成的类型工会的声明
  是可见的。两种结构共享的公共初始序列的如果对应成员
  具有兼容的类型(并且,对于位字段,相同的宽度)为一个或多个的序列
  最初的成员​​。

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. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

(C ++使得标准布局工会同担保(C ++ 11,9.2 / 18))。

(C++ makes the same guarantee (C++11, 9.2/18) for standard-layout unions.)

这篇关于共同第一部件结构的联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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