struct packing:如何在开头添加struct成员?

struct packing: how to add struct members at the beginning?

我正在用 C89 实现一个二叉树,我试图通过组合在所有节点结构之间共享公共属性。因此我有以下代码:

enum foo_type
{
    FOO_TYPE_A,
    FOO_TYPE_B
};

struct foo {
    enum foo_type type;
};

struct foo_type_a {
    struct foo base;
    struct foo * ptr;
};

struct foo_type_b {
    struct foo base;
    char * text;
};

我在所有结构定义中包含类型 struct foo 的成员作为它们的初始成员,以便提供对 enum foo_type 持有的值的访问,而不管结构类型如何。为实现这一点,我希望指向结构对象的指针指向其初始成员,但我不确定这种假设在这种情况下是否成立。对于 C99,标准规定如下(参见 ISO/IEC 9899:1999 6.7.2.1 §13)

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.

尽管所有结构共享一个公共 struct foo 对象作为它们的初始成员,填充开始发挥作用。虽然 struct foo 只有一个与 int 大小相同的成员,但 struct foo_type_astruct foo_type_b 都包含指针成员,这在某些情况下会增加对齐,从而增加填充。

因此,考虑到这种情况,C 编程语言(C89 或任何后续版本)是否确保通过指向对象的指针访问 struct foo::type 的值是安全的,无论该对象的类型是什么struct foo 或包含类型为 struct foo 的对象作为其第一个成员,例如 struct foo_type_astruct foo_type_b

正如您自己引用的 C 标准,您描述的内容受 C99 及更高版本支持。

C89 似乎也支持它,因为您引用的语言自 1988 年以来就已存在于 ANSI-C 文档中:

3.5.2.1 Structure and union specifiers

...

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 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. There may therefore be unnamed holes within a structure object, but not at its beginning, as necessary to achieve the appropriate alignment.