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) ?
sizeof(expr_t)equals the value thatoffsetof(expr_t, children)if you had redefinedchildrento be an array of any determinate size.