Packing of Struct Fields

Per Nordlöw per.nordlow at gmail.com
Sat Oct 17 12:44:44 UTC 2020


On Saturday, 17 October 2020 at 12:35:37 UTC, Per Nordlöw wrote:
> On Friday, 16 October 2020 at 21:26:12 UTC, Steven 
> Schveighoffer wrote:
>> To further explain this -- the padding is added so things like 
>> pointer arithmetic on an array work.
>
> In my code sample above one can only access the first element 
> anyhow so I don't understand why this restriction is imposed 
> here.
>
> struct S
> {
>     int i;
>     bool b;
> }
>
> struct T
> {
>     S s; // reinterpreting this as an array can only access 
> this first element anyway
>     char c; // so why can't this be aligned directly after `s` 
> without any padding?
> }

So AFAICT the key question becomes:

Can `align`s be inserted in S or/and T so that T is packed to 8 
bytes but still aligned to 8 bytes? I don't see why this 
shouldn't be the default behaviour...


More information about the Digitalmars-d-learn mailing list