From 6.7.9p23:
The estimates of the initialization list expressions are vaguely ordered using each other [...] (152) In particular, the evaluation order should not be the same as the order of initialization of a subobject.
Thus, there is no guarantee that a.size initialized at the point calloc(a.size, 1) is evaluated to initialize a.buf .
In this case, the creation function is a suitable initializer:
inline buff_t create_buff(u_int32_t size) { return (buff_t) {.size = size, .buf = calloc(size, 1)}; } buff_t a = create_buff(20);
This cannot be used for objects in a static or file area; in this case, you need a macro (or, for example, an expression of the gcc operator, which can be used in a macro).
source share