C11 states: 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. Consider the following C program: struct S { char c; int x[]; // flexible array member }; int printf(const char*, ...); int main(){ _Alignas(int) char buff[sizeof(struct S) + sizeof(int[8])]; struct S* s = (struct S*)buff; printf("%zu\n", sizeof(buff)); // prints 56, should print 36 on 64 bit printf("%zu\n", sizeof(struct S)); // should print 4, prints 24 on 64bit. for(int i = 0; i < 8; i++) s->x[i] = i; // program segfaults here return 0; } I have to compile with -betterC to get it to compile at all (errors with Error: `tupleof` is not a member of `const(S)`, Error: invalid `foreach` aggregate `_error_` without it). When it does compile, it gives incorrect results. I’m guessing it is treating it as a D dynamic array (which would explain the incorrect size).
@WalterBright created dlang/dmd pull request #13095 "fix Issue 22326 - ImportC: struct with flexible array member is incor…" fixing this issue: - fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled https://github.com/dlang/dmd/pull/13095
dlang/dmd pull request #13095 "fix Issue 22326 - ImportC: struct with flexible array member is incor…" was merged into stable: - 0494c010c3d621a08e24715c33980fd3cedb8675 by Walter Bright: fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled https://github.com/dlang/dmd/pull/13095
dlang/dmd pull request #13108 "merge stable" was merged into master: - 03c69439f8525ec0b32f76463e86adc74c6c6383 by Walter Bright: fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled (#13095) https://github.com/dlang/dmd/pull/13108