Pointer in structure memory allocation in initialization (c99)
c, c99, pointers
Solution
From 6.7.9p23:
The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another [...] (152) In particular, the evaluation order need not be the same as the order of subobject initialization.
So there is no guarantee that `a.size` is initialized at the point `calloc(a.size, 1)` is evaluated for the initialization of `a.buf`.
In this case, a suitable initializer would be a creation function:
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 can't be used for static or file-scope objects; in that case a macro would be necessary (or, for example, a gcc statement expression, which could be used in a macro).
Problem
Lets assume that we've got a type: ``` typedef struct __BUFF_T__ { u_int8_t *buf; u_int32_t size; }buff_t; ``` Is it correct allocating memory next way in c99? ``` buff_t a = {.size = 20,.buf = calloc(a.size,1)}; ``` Compiler shows warning Variable 'data' is uninitialized when used within its own initialization Memory's available and all, but are there some other non-warning options to do the same?