On quinta-feira, 28 de setembro de 2017 11:16:19 PDT Myriachan wrote:
> On Wednesday, September 27, 2017 at 9:14:47 PM UTC-7, Chris Hallock wrote:
> > On Wednesday, September 27, 2017 at 4:28:53 PM UTC-4, Myriachan wrote:
> >> For a union U with a non-static member of type T, is it guaranteed by the
> >> Standard that alignof(U) >= alignof(T)?
> >
> > Apparently not; according to [basic.align]/2
> > <
http://eel.is/c++draft/basic.align#2>, "[...] The alignment required for
> > a type might be different when it is used as the type of a complete object
> > and when it is used as the type of a subobject. [...] The result of the
> > alignof operator reflects the alignment requirement of the type in the
> > complete-object case."
>
> Is taking a pointer to the subobject legal, then? The pointer would not be
> properly aligned.
Again, the question is what "properly aligned" mean. From the HW point of
view, it's properly aligned if the load, store and modify operations that the
compiler will issue will succeed without an alignment fault/trap. In that
case, yes, they will be properly aligned. Since x86 didn't originally require
data alignment, unlike the RISC machines of the time, and no one turns on the
AC flag anyway, the required alignment for all types on x86 is actually 1.
But there's also "optimally aligned", which that's a completely different
subject. The optimal alignment of a 1-byte spinlock is 64 bytes, for example
(more specifically: no other spinlocks on the same cacheline). For 8- and 12-
byte quantities, the optimal alignment is 8 bytes. That's what alignof(double)
has returned for the past 15 years.
But it didn't use to. In the olden days, it returned 4. So structures
containing doubles and long longs were also aligned to 4 and the padding
inside those structures was suitable for that. That cannot change.