结构和用C工会,确定大小和访问成员 [英] Structures and Unions in C, determining size and accessing members

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

问题描述

所有,

下面是我觉得这混乱的工会的例子。

Here is an example on Unions which I find confusing.

struct s1
{
    int a;
    char b;
    union
    {
       struct
       {
          char *c;
          long d;
       }
       long e;
     }var;
};

考虑到字符是1个字节, INT 为2个字节,为4个字节。什么是这里的整个结构的大小?将工会大小为{字符*的大小} + {双倍大小}?我是因为包裹在联盟结构的困惑。

Considering that char is 1 byte, int is 2 bytes and long is 4 bytes. What would be the size of the entire struct here ? Will the union size be {size of char*}+ {size of double} ? I am confused because of the struct wrapped in the union.

另外,我怎么能访问变量 D 的结构。 var.d

Also, how can I access the variable d in the struct. var.d ?

推荐答案

由于没有填充,并假设的sizeof(int)的==的sizeof(字符*)==的sizeof(长)== 4,外结构的大小将是13。

With no padding, and assuming sizeof(int)==sizeof(char *)==sizeof(long)==4, the size of the outer struct will be 13.

其分解,结合 VAR 重叠与单个匿名结构。这内部结构较大(指针和长),所以它的大小控制了工会的大小,使工会消耗8个字节。其他成员是4个字节,1字节,所以总为13

Breaking it down, the union var overlaps an anonymous struct with a single long. That inner struct is larger (a pointer and a long) so its size controls the size of the union, making the union consume 8 bytes. The other members are 4 bytes and 1 byte, so the total is 13.

在任何合理的实施与大小假设我上面所做,这个结构将被填充,选择两字节或4字节的边界,将至少1个或3个附加字节大小。

In any sensible implementation with the size assumptions I made above, this struct will be padded to either 2 byte or 4 byte boundaries, adding at least 1 or 3 additional bytes to the size.

编辑:在一般情况下,由于所有的成员类型的大小本身实现定义,定义的填充实现,您需要参考的文档的实现和平台知道。

In general, since the sizes of all of the member types are themselves implementation defined, and the padding is implementation defined, you need to refer to the documentation for your implementation and the platform to know for sure.

的实施允许一个结构基本上任何元素后插入填充。明智的实现使用作为必须遵守的平台要求(例如,RISC处理器通常需要的值被调整到该值的大小),或表现为小填充。

The implementation is allowed to insert padding after essentially any element of a struct. Sensible implementations use as little padding as required to comply with platform requirements (e.g. RISC processors often require that a value is aligned to the size of that value) or for performance.

如果使用的是结构映射到由文件格式规范,共享内存协处理器,硬件设备,或在包装和布局实际的事情,那么你可能要被关注任何类似的情况下,假设值的布局领域您在编译时或运行时,你的成员布局假设是真实的测试。这可以通过验证整个结构的尺寸,以及其成员的偏移量来完成。

If using a struct to map fields to the layout of values assumed by file format specification, a coprocessor in shared memory, a hardware device, or any similar case where the packing and layout actually matter, then you might want to be concerned that you are testing at either compile time or run time that your assumptions of the member layout are true. This can be done by verifying the size of the whole structure, as well as the offsets of its members.

请参阅这个问题其中包括为编译时断言技巧的讨论。

See this question among others for a discussion of compile-time assertion tricks.

这篇关于结构和用C工会,确定大小和访问成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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