具有灵活数组成员的结构的大小

Size of a struct with flexible array member

给出

struct Foo {
    uint32_t a;
    uint32_t b[];
};

什么是sizeof(Foo)?它是实现定义的还是未定义的行为? C 与 C++ 的答案是否不同?

编译器将忽略灵活数组成员,因为它不存在。

C11-§6.7.2.1 (p18)

[...] In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply [...].

据我所知,直到 c++14,灵活的数组成员才不是 C++ 标准的一部分。但是,GNU 支持它作为 an extension。 C 和 C++ 的行为相似。