8
votes

Say, I have an aligned array

alignas(X) char arr[sizeof(X)];

Is the pointer

X * ptr = reinterpret_cast<X*>(arr);

guaranteed to be properly aligned according to the alignment requirements of X?

It seems obvious that this is the case, however it does not appear to be obvious from the C++ standard.

I don't find anything in the standard that would prevent a compiler to align a struct of the form

struct X
{
    int16_t a;
    int32_t b;
    int16_t c;
};

like this in memory:

+-+-+-+-+-+-+-+-+
|a|a|b|b|b|b|c|c|
+-+-+-+-+-+-+-+-+
     ^
     aligned to 32-boundary

so that the objects of type X are aligned in such a way that no padding is needed and b is aligned correctly at the same time to a 32-bit boundary. The statement (N4713, § 6.6.5.1)

An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated.

is not violated by that and I don't see any other statement in the standard that violates it.

2
I'm confused by the question, but maybe I'm not understanding it correctly. Are you imagining a scenario where X contains no padding but where int32_t is required to have 32 bit alignment? And are you asking which part of the standard prevents this case? - François Andrieux
I guess the alignas refers to memory alignment from the start of the structure, not related to the actual placement of the structure in memory. - Michael Chourdakis
@FrançoisAndrieux Yes, that is exactly what I am asking. - Ralph Tandetzky
@RalphTandetzky I'm not sure if that has an explicit passage in the standard. It may be implicit from other language rules, for example the fact that X x; auto y = &x.b; needs to produce a usable pointer to b, likely as part of the definition of how the address of operator is defined by the language. - François Andrieux
How will b be aligned to a 32 bit boundary, if the start of the structure is already aligned to a 32 bit boundary (e.g. coming from malloc or new). b being aligned to 32 bits (on an 8 bit byte addressable system) requires the start of the structure being aligned to an address that leaves a reminder of 2 when divided by 4. - Kaz

2 Answers

2
votes

Your structure should have an exotic alignment: it should be 16 bit aligned but not 32 aligned. Mathematically speaking, its address should be of the form 32*n+16, with n an integer.

This is forbidden by the language, a 32 bit alignment should satisfy a 16 bit alignment. Said differently any 16bit aligned structure should fit in a 32 bit aligned storage region, but your 16bit aligned structure don't. See [basic.align]/5:

Alignments have an order from weaker to stronger or stricter alignments. Stricter alignments have larger alignment values. An address that satisfies an alignment requirement also satisfies any weaker valid alignment requirement.

1
votes

The authors of the Standard didn't seek to explicitly prohibit things they thought were impossible. If one compilation units contains a structure like the one you gave:

struct {
  uint16_t a;
  uint32_t b;
  uint16_t c;
} x;

and a compiler isn't omniscient about all the ways that x or structures with identical layout might be used throughout the rest of the program, it will have no choice but to make the offset of b be a multiple of b's alignment. I don't think the Standard explicitly says that the layout of a structure cannot be observably affected by the ways in which it is used, but I think that's because they didn't think a compiler where layouts could vary in that fashion would be able to uphold the Common Initial Sequence guarantees.