How to fix the mismatch struct size

Arjan arjan at ask.me.to
Mon May 16 15:28:42 UTC 2022


On Monday, 16 May 2022 at 12:51:01 UTC, test123 wrote:
> On Monday, 16 May 2022 at 07:29:39 UTC, Arjan wrote:
>> On Monday, 16 May 2022 at 05:57:33 UTC, test123 wrote:
>> Without fully scrutinizing your code I suspect the issues you 
>> see are caused by:
>> - alignment
>
> I know it is cause by alignment. my problem how to deal with 
> diff alignment with diff platform. special when there is multi 
> level nested struct.

Nesting does not make a difference in c/c++, it does however in D.

>
>> - struct context pointer
>
> There is no struct context pointer since I translate all nested 
> struct and union into static.

That is not shown in the code.

>
> struct work for linux with clang, but not for windows.

Its it not clear what you want to achieve, assuming you want 
these structs to be equal (binary compatible layout on systems 
with same endianness)

- 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






More information about the Digitalmars-d mailing list