简体   繁体   English

struct alignment C / C ++

[英]struct alignment C/C++

In c/c++ (I am assuming they are the same in this regard), if I have the following: 在c / c ++中(我假设它们在这方面是相同的),如果我有以下内容:

struct S {
  T a;
  .
  .
  .
} s;

Is the following guaranteed to be true? 以下是否保证是真的?

(void*)&s == (void*)&s.a;

Or in other words, is there any kind of guarantee that there will be no padding before the first member? 或者换句话说,是否有任何保证第一个成员之前没有填充?

In C, yes, they're the same address. 在C中,是的,它们是相同的地址。 Simple, and straightforward. 简单明了。


In C++, no, they're not the same address. 在C ++中,不,它们不是同一个地址。 Base classes can (and I would suspect, do) come before all members, and virtual member functions usually add hidden data to the struct somewhere. 基类可以(我怀疑,确实)来到所有成员之前,而虚拟成员函数通常会将隐藏数据添加到某个地方的结构中。 Even more confusing, a C++ compiler may also rearrange members at will, unless the class is a standard layout type (though I don't know that any compiler does so) 更令人困惑的是,C ++编译器也可以随意重新排列成员,除非该类是标准布局类型(虽然我不知道任何编译器都这样做)

Finally, if the C++ struct is composed of standard layout types , contains no base classes nor virtual functions and all members have the same visibility, and possibly other limitations I forgot, then it falls back on the C rules, and requires the first member to be at the same address as the object itself. 最后,如果C ++结构由标准布局类型组成,不包含基类和虚函数,并且所有成员都具有相同的可见性,并且可能还有其他我忘记的限制, 那么它将依赖于C规则,并且需要第一个成员与对象本身位于同一地址。

§ 9.2/7 §9.2/ 7

A standard-layout class is a class that: 标准布局类是一个类:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference, - 没有非标准布局类(或此类类型的数组)或引用类型的非静态数据成员,
— has no virtual functions (10.3) and no virtual base classes (10.1), - 没有虚函数(10.3),没有虚基类(10.1),
— has the same access control (Clause 11) for all non-static data members, - 对所有非静态数据成员具有相同的访问控制(第11条),
— has no non-standard-layout base classes, - 没有非标准布局基类,
— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and - 在大多数派生类中没有非静态数据成员,并且最多只有一个具有非静态数据成员的基类,或者没有具有非静态数据成员的基类,并且
— has no base classes of the same type as the first non-static data member. - 没有与第一个非静态数据成员相同类型的基类。

§ 9.2/20 §9.2/ 20

A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. 指向标准布局结构对象的指针(使用reinterpret_cast进行适当转换)指向其初始成员(或者如果该成员是位字段,则指向它所在的单元),反之亦然。 [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. [注意:因此,在标准布局结构对象中可能存在未命名的填充,但不是在其开头,以实现适当的对齐。 —end note ] - 尾注]

Yes, it is. 是的。

It is guaranteed there is no padding before the first struct member in C and in C++ (if it is a POD). 保证在C和C ++中的第一个结构成员之前没有填充(如果它是POD)。

C quote: C报价:

(C11, 6.7.2.1p15) "There may be unnamed padding within a structure object, but not at its beginning." (C11,6.7.2.1p15)“结构对象中可能有未命名的填充,但不是在它的开头。”

C++ quote: C ++引用:

(C++11, 9.2p20) "There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment" (C ++ 11,9.2p20)“因此,在标准布局结构对象中可能存在未命名的填充,但在开始时可能没有,以实现适当的对齐”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM