Is it guaranteed by the standard?
Formally, no; in practice, yes. The closest to a guarantee is C++11 9.2/20:
A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast
, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]
So the cast will correctly reinterpret the first array elements as a single structure; but I don't think there's a formal guarantee that it won't expect extra padding between structures.
If not, is it true in practice for Intel, AMD, and ARM?
At least on mainstream architectures like the ones you list, there's no need to add padding for alignment in this case, and no other reason to do so; so in practice, this should work, but without guaranteed portability.
I found there is an __attribute__((__packed__))
for GCC, would that guarantee it for GCC at least?
Yes. According to the documentation, this "specifie[s] that the minimum required memory be used to represent the type", so it will prevent any padding being added after the array member.
Does MSVC have a similar option?
It has a compiler flag /Zp1
and a pragma #pragma pack(push, 1)
(with #pragma pack(pop)
to restore default alignment), but they don't seem to offer the guarantee you need; they control the alignment of "each structure member after the first is stored", with no mention of the overall structure size.