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;
}
?
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 t
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 layout
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;
}
?
S.size
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
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 ele
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.
I
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 s.
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
{
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. I
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
On Saturday, 17 October 2020 at 13:23:38 UTC, Adam D. Ruppe wrote:
Use a union.
Nice! Thanks!
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[] wi
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.
14 matches
Mail list logo