This is C99 code:
typedef struct expr_t
{
int n_children;
foo data; // Maybe whatever type with unknown alignment
struct expr_t *children[];
} expr_t;
Now, how do I allocate memory ?
expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *));
or
expr_t *e = malloc (offsetof (expr_t, children) + n * sizeof (expr_t *));
?
Is sizeof
even guaranteed to work on an type with flexible array member (GCC accepts it) ?
If the compiler accepts the declaration of a struct with a flexible array member, the
sizeof
operator for that struct should yield the size of the struct as if the flexible array member doesn't exist.The correct allocation of such a struct would be:
You can still do this trick even if flexible array members are not supported by the compiler. Just declare the array member of your struct as having a size of
1
, and then allocaten - 1
items instead ofn
.expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *));
is well defined in C99. From the C99 specification 6.7.2.1.16: