Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) ?

like image 586
Alexandre C. Avatar asked Oct 01 '12 20:10

Alexandre C.


1 Answers

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:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

like image 107
CrazyCasta Avatar answered Sep 30 '22 14:09

CrazyCasta