Cstruct data types may end with aflexiblearray member[1] with no specified size:
structvectord{shortlen;// there must be at least one other data memberdoublearr[];// the flexible array member must be last// The compiler may reserve extra padding space here, like it can between struct members};
Typically, such structures serve as the header in a larger,variable memory allocation:
structvectord*vector=malloc(...);vector->len=...;for(inti=0;i<vector->len;i++)vector->arr[i]=...;// transparently uses the right type (double)
Thesizeof
operator on such astruct
gives the size of the structure as if the flexible array member were 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.[2]
It is common to allocatesizeof(struct) +array_len*sizeof(array element)
bytes.
This is not wrong, but it may allocate a few more bytes than necessary: the compiler may be re-purposing some of the padding that is included insizeof(struct)
. Should this be a concern, macros are available[3] 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 (arr[i]
) oroffsetof
, notsizeof
.
Flexible array members were officially standardized inC99.[4] In practice, compilers (e.g.,GCC,[5]MSVC[6]) provided them well before C99 was standardized.
Flexible array members are not officially part ofC++, but language extensions[7] are widely available.
A zero-sized array is legal only when the array is the last field in a struct or union and when the Microsoft extensions (/Ze) are enabled.