How to fix the mismatch struct size

test123 test123 at gmail.com
Tue May 17 09:40:40 UTC 2022


On Monday, 16 May 2022 at 15:28:42 UTC, Arjan wrote:
>
> - enforce in C/C++ the alignment to byte/word/dword/qword 
> alignment. This could be done with compiler `#pragma pack` or 
> from C++/11 `alignas` or c/11 `_Alignas` (see 
> https://en.cppreference.com/w/cpp/language/alignas)
> to make sure the padding is the same.
> - **do not** use bitfields since the packing of adjacent fields 
> is implementation (compiler) dependent. (most probably the 
> cause for diff in size 16 <==> 24 as you observed) see 
> https://en.cppreference.com/w/cpp/language/bit_field
> - make sure the type's used in the struct are architecture 
> independent, size of int in c/c++ is dependent on target arch 
> x86=> 32bits x86_64 64bits. Its fixed in D.
> - make sure the size_t is the same type and size for all 
> platforms
> - use static struct in D
> - use the correct corresponding types 
> https://dlang.org/spec/type.html in D or use core.stdc.stdint 
> type aliases

I can not change c struct since I don't own the project but need 
deal with it.

All subtype size is matched that is why posix test work (and also 
check offsetof is matched).

The problem is even all subtype size is same, but not all 
platform has same memory layout.


More information about the Digitalmars-d mailing list