Reputation: 797
T
that I explicitly specify as x-alignedsizeof(T)
Suppose I now have: T arr[y]
, where arr
is x-aligned (either by being allocated on the stack, or in data, or by an x-aligned heap allocation)
Then at least some of arr[1],...,arr[y-1]
are not x-aligned.
Correct? (In fact, it must be correct if sizeof(T)
does not change with extended alignment specification)
Note1: This is not the same question as How is an array aligned in C++ compared to a type contained?. This question asks about the alignment of the array itself, not of the individual elements inside.
Note2: This question: Does alignas affect the value of sizeof? is essentially what I'm asking - but for extended-alignment.
Note3: https://stackoverflow.com/a/4638295/7226419 Is an authoritative answer to the question (that sizeof(T)
includes any padding necessary to satisfy alignment requirements for having all T
's in an array of T
's properly aligned.
Upvotes: 1
Views: 1778
Reputation: 119877
If type T
is x-aligned, every object of type T
is x-aligned, including any array elements. In particular, this means x > sizeof(T)
cannot possibly hold.
A quick test with a couple of modern compilers confirms:
#include <iostream>
struct alignas(16) overaligned {};
struct unaligned {};
template <class T> void sizes()
{
T m, marr[2];
std::cout << sizeof(m) << " " << sizeof(marr) << std::endl;
}
int main ()
{
sizes<unaligned>();
sizes<overaligned>();
}
Output:
1 2
16 32
Upvotes: 10