Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
On Saturday, 17 October 2020 at 13:42:46 UTC, Steven Schveighoffer wrote: I think it *should* be possible to do this, if it's not already, just with pragmas. (i.e. pack T but not S). Agree, a pragma, say `pragma(pack)`, to control this would be great to avoid the unsafe union hack.

Re: Packing of Struct Fields

2020-10-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/17/20 9:00 AM, Per Nordlöw wrote: On Saturday, 17 October 2020 at 12:51:21 UTC, ag0aep6g wrote: c does come directly after s. The padding between b and c is part of s. If you don't want that padding, you can use `align(1)` to define S without padding. But then 75% of the ints in an S[]

Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
On Saturday, 17 October 2020 at 13:23:38 UTC, Adam D. Ruppe wrote: Use a union. Nice! Thanks!

Re: Packing of Struct Fields

2020-10-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 17 October 2020 at 13:00:59 UTC, Per Nordlöw wrote: I understand that. I don't want the alignment of `S` to change. I want the padding after `s` That padding is part of S. It is at the end, after its fields, but still part of it. S's layout doesn't depend on what else is around

Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
On Saturday, 17 October 2020 at 12:51:21 UTC, ag0aep6g wrote: c does come directly after s. The padding between b and c is part of s. If you don't want that padding, you can use `align(1)` to define S without padding. But then 75% of the ints in an S[] will be misaligned. I understand that.

Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
On Saturday, 17 October 2020 at 12:44:44 UTC, Per Nordlöw wrote: 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... I though this would do the trick but not... struct S {

Re: Packing of Struct Fields

2020-10-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.10.20 14:35, Per Nordlöw wrote: 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? } c does come directly after

Re: Packing of Struct Fields

2020-10-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 17 October 2020 at 12:44:44 UTC, Per Nordlöw wrote: Can `align`s be inserted in S or/and T so that T is packed to 8 bytes but still aligned to 8 bytes? Yes. Put an align on the OUTSIDE of the struct you are nesting, then put one INSIDE the struct you want the contents packed.

Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
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

Re: Packing of Struct Fields

2020-10-17 Thread Per Nordlöw via Digitalmars-d-learn
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

Re: Packing of Struct Fields

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 4:44 PM, ag0aep6g wrote: On 16.10.20 22:32, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S { int i; bool b; } struct T { S s; char c; } struct U { int i; bool b; char c; } ?

Re: Packing of Struct Fields

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:32 PM, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? I have a function that dumps member

Re: Packing of Struct Fields

2020-10-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.10.20 22:32, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? S.sizeof: 4 bytes for the int + 1 byte for

Packing of Struct Fields

2020-10-16 Thread Per Nordlöw via Digitalmars-d-learn
Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S { int i; bool b; } struct T { S s; char c; } struct U { int i; bool b; char c; } ?