On Thu Jan 18 13:22:54 2024 +0000, Yuxuan Shui wrote:
`calloc(1, offsetof(struct bla, array[count]))`
That's not how the C standard shows how ~you should~ one would typically allocate struct with flexible member. This is the example:
int m = /* some value */; struct s *p = malloc(sizeof(struct s) + sizeof(double [m]));
I think worst case here is we would allocate a few more bytes?
Which is the point of asserting the struct size: it will be impossible to mis-allocate if it doesn't have implicit padding, and make undefined behavior impossible to happen, and that regardless how you do the allocation.
Then we do such allocations very consistently using offsetof, which makes sure that any type change of the array will be handled properly.