C bitfields guarantees

Tim tim.dlang at t-online.de
Fri Jul 5 19:56:49 UTC 2024


On Friday, 5 July 2024 at 19:35:10 UTC, Steven Schveighoffer 
wrote:
>
> What if you need > 32 bits or want to pack into a `ulong`? Is 
> the behavior sane across compilers?

The following struct has a different layout for different 
platforms:
```
struct S { unsigned int x; unsigned long long a:20, b:20, c:24; };
```

Windows layout:
```
          0 | struct S
          0 |   unsigned int x
     8:0-19 |   unsigned long long a
    10:4-23 |   unsigned long long b
    13:0-23 |   unsigned long long c
            | [sizeof=16, align=8]
```

Linux x86_64 layout:
```
          0 | struct S
          0 |   unsigned int x
     4:0-19 |   unsigned long long a
     8:0-19 |   unsigned long long b
    10:4-27 |   unsigned long long c
            | [sizeof=16, align=8]
```

Linux i686 layout:
```
          0 | struct S
          0 |   unsigned int x
     4:0-19 |   unsigned long long a
     6:4-23 |   unsigned long long b
     9:0-23 |   unsigned long long c
            | [sizeof=12, align=4]
```



More information about the Digitalmars-d mailing list