C工会从一开始就不会填充过吗? [英] Are C unions never padded at the beginning?

查看:63
本文介绍了C工会从一开始就不会填充过吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99标准中,是否有任何保证只能将struct像结构那样最后填充工会?并且相关地,联合会的地址是否始终等于其任何可能的成员的地址?

Are there any guarantees in the C99 standard that unions will only ever be padded at the end like structs? And relatedly, will the address of the union always be equal to the address of any of its possible members?

推荐答案


  1. 是。如您所述,结构永远不会有领先的填充。联合的地址始终指联合的任何组成部分的第一个元素(带有适当的强制转换),因此联合的开始也不能有填充。

  1. Yes. As you note, structures never have leading padding. The address of a union always refers to the first element of any component of the union (with suitable casts), so there can't be any padding at the start of a union either.

是。适当地强制转换,联合的地址也是联合中任何元素的指针。

Yes. Suitably cast, the address of a union is also a pointer to any of the elements within the union.




ISO / IEC 9899:2011



6.7.2.12结构和联合说明符



¶15在结构对象中,非位域成员和位域
所在的单元的地址按声明的顺序增加。指向
结构对象(经过适当转换)的指针指向其初始成员(或者,如果该成员是
位域,则指向它所驻留的单元),反之亦然。

ISO/IEC 9899:2011

6.7.2.12 Structure and union specifiers

¶15 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

¶16一个联合体的大小足以容纳其最大的填充物。成员。
中的大多数成员的值可以随时存储在并集对象中。适当地转换指向
联合对象的指针指向其每个成员(或者,如果成员是位域,则
则指向它所驻留的单元),反之亦然。

¶16 The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bitfield, then to the unit in which it resides), and vice versa.

¶17在结构或联合的末尾可能会有未命名的填充。

¶17 There may be unnamed padding at the end of a structure or union.

这篇关于C工会从一开始就不会填充过吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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