Flexible array member


data types may end with a flexible array member with no specified size:

struct vectord ;

Typically, such structures serve as the header in a larger, variable memory allocation:

struct vectord *vector = malloc;
vector->len =...;
for
vector->arr =...; // transparently uses the right type

Effect on struct size and padding

The sizeof operator on such a struct gives the size of the structure as if the flexible array member was empty. This may include padding added to accommodate the flexible member; the compiler is also free to re-use such padding as part of the array itself.
It is common to allocate sizeof + array_len*sizeof bytes.
This is not wrong, however it may allocate a few more bytes than necessary: the compiler may be re-purposing some of the padding that is included in sizeof. Should this be a concern, macros are available to compute the minimum size while ensuring that the compiler's padding is not disrupted.
As the array may start in the padding before the end of the structure, its content should always be accessed via indexing or offsetof, not sizeof.

Availability

Flexible array members were officially standardized in C99, however compilers accepted zero-sized array members with the same effect.
Flexible array members are not officially part of C++, but the same compatibility extensions exist.