Search code examples
csizeofflexible-array-member

What is the size of a struct with a flexible array member?


Given

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

What is sizeof(Foo)? Is it implementation-defined or undefined behaviour? Does the answer differ for C vs C++?


Solution

  • The compiler will ignore the flexible array member as it were not there.

    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 [...].

    AFAIK, flexible array member is not the part of C++ standard till c++14. But, GNU support it as an extension. Behaviour will be similar for both C and C++.