4 ms·
> In C99 it is allowed to declare the last member of a structure as an array with no number of elements specified. The size of the struct will then be as if the
by defen 3y ago
> In C99 it is allowed to declare the last member of a structure as an array with no number of elements specified. The size of the struct will then be as if the last member did not exist.
There's one gotcha here, which is that the alignment requirements of the flexible array member can change the size of the struct. For example the following fails on x86_64-linux-gnu:
struct flexible_char {
char c;
char arr[];
};
struct flexible_int {
char c;
int arr[];
};
_Static_assert(sizeof(struct flexible_char) == sizeof(struct flexible_int), "size mismatch");
Because `struct flexible_int` needs to be 4-byte aligned but `struct flexible_char` only needs to be 1-byte aligned.