S
sp12341234
Are all unions required to not have leading padding? It's common for
them not to, but I don't think it is required by the standard. I'm
using N1256.
6.7.2.1#15 says
There may be unnamed padding at the end of a structure or union.
Some people might take the omission of "there can be leading padding"
to mean there can't be leading padding. But I don't think that is
true. 4.2 says
If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a
constraint is violated, the behavior is undefined. Undefined behavior
is otherwise indicated in this International Standard by the words
‘‘undefined behavior’’ or by the omission of any explicit definition
of behavior. There is no difference in emphasis among these three;
they all describe ‘‘behavior that is undefined’’.
Since there is no explicit definition on whether there can be leading
padding in a union, it's therefore undefined behavior to rely on the
presence or absence of leading padding, therefore an implementation
could have it.
6.7.2.1#14 says
[...] A pointer to a union object, suitably converted, points to each
of its members (or if a member is a bit-field, then to the unit in
which it resides), and vice versa.
Some people might think this means that the beginning of a union must
be at the same place as its member. I don't think this is true. An
implementation could make conversions from a pointer to union to a
pointer to one of its members undergo a "leading padding correction"
where it adds and subtracts padding from the pointer value as
necessary. This is supported by the conjunction of 6.2.3 and pointers
to union types and pointers to their members not being compatible
types in 6.2.7.
6.5.8#5 says
[...] All pointers to members of the same union object compare equal.
[...]
So all members must have the same amount of leading padding.
6.3.2.3#7 says
[...] When a pointer to an object is converted to a pointer to a
character type, the result points to the lowest addressed byte of the
object. Successive increments of the result, up to the size of the
object, yield pointers to the remaining bytes of the object.
If a union has a character type member, a cast from a pointer to union
to a pointer to character type would have to point to both the
beginning of the union and the character type member. Therefore, the
character type member couldn't have leading padding. Since all members
have to start at the same spot, no members could have leading padding
if the union had a character type member.
But what about unions without a character type member? They could
still have padding, right? Am I missing something?
Thanks.
--
them not to, but I don't think it is required by the standard. I'm
using N1256.
6.7.2.1#15 says
There may be unnamed padding at the end of a structure or union.
Some people might take the omission of "there can be leading padding"
to mean there can't be leading padding. But I don't think that is
true. 4.2 says
If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a
constraint is violated, the behavior is undefined. Undefined behavior
is otherwise indicated in this International Standard by the words
‘‘undefined behavior’’ or by the omission of any explicit definition
of behavior. There is no difference in emphasis among these three;
they all describe ‘‘behavior that is undefined’’.
Since there is no explicit definition on whether there can be leading
padding in a union, it's therefore undefined behavior to rely on the
presence or absence of leading padding, therefore an implementation
could have it.
6.7.2.1#14 says
[...] A pointer to a union object, suitably converted, points to each
of its members (or if a member is a bit-field, then to the unit in
which it resides), and vice versa.
Some people might think this means that the beginning of a union must
be at the same place as its member. I don't think this is true. An
implementation could make conversions from a pointer to union to a
pointer to one of its members undergo a "leading padding correction"
where it adds and subtracts padding from the pointer value as
necessary. This is supported by the conjunction of 6.2.3 and pointers
to union types and pointers to their members not being compatible
types in 6.2.7.
6.5.8#5 says
[...] All pointers to members of the same union object compare equal.
[...]
So all members must have the same amount of leading padding.
6.3.2.3#7 says
[...] When a pointer to an object is converted to a pointer to a
character type, the result points to the lowest addressed byte of the
object. Successive increments of the result, up to the size of the
object, yield pointers to the remaining bytes of the object.
If a union has a character type member, a cast from a pointer to union
to a pointer to character type would have to point to both the
beginning of the union and the character type member. Therefore, the
character type member couldn't have leading padding. Since all members
have to start at the same spot, no members could have leading padding
if the union had a character type member.
But what about unions without a character type member? They could
still have padding, right? Am I missing something?
Thanks.
--