Allocating struct with flexible array member
问题 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) ? 回答1: expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); is well defined