Allocating struct with flexible array member

冷暖自知 提交于 2019-11-27 06:06:47

问题


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 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.




回答2:


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:

expr_t *e = malloc (sizeof(expr_t) + n * sizeof(struct expr_t *));

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 allocate n - 1 items instead of n.



来源:https://stackoverflow.com/questions/12680946/allocating-struct-with-flexible-array-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!