Issue 1041: Alignment of structure and union members

Authors: Jay Ghiron
Date: 2026-04-02
Submitted against: C23
Status: Open
Cross-references: 1044

The definition of common initial sequences fails to mention alignment:

One special guarantee is made to simplify the use of unions: if a union contains several structures that share a common initial sequence (see following sentence), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

(C23 6.5.3.4 "Structure and union members" paragraph 6.)

Meaning that currently, the following two structures have all of their members in the common initial sequence:

struct A{
int x;
int y;
};
struct B{
int x;
alignas(alignof(int)*2)int y;
};

That is, alignas cannot change how padding is inserted within a structure. No compilers actually follow this, however. The definition of type compatibility requires alignments to be the same:

Moreover, two complete structure, union, or enumerated types declared with the same tag are compatible if members satisfy the following requirements:

...

(C23 6.2.7 "Compatible type and composite type" paragraph 1.)

It is not clear what "equivalent alignment specifier" means. Consider the following four translation units:

struct S{int x;};/* TU 1 */

struct S{alignas(0)int x;};/* TU 2 */

struct S{alignas(int)int x;};/* TU 3 */

struct S{alignas(alignof(int))int x;};/* TU 4 */

GCC considers all four structure types to be compatible, even though it seems clear that the first structure type should not be compatible with the third structure type or fourth structure type. The first two structure types being compatible is less clear, the second has an alignment specifier and the first does not but alignas(0) is described as having no effect.

Question 1

Is it intended for common initial sequences to consider alignment requirements specified by alignas?

Question 2

What does "equivalent alignment specifier" mean? What if there are multiple alignment specifiers? Should an alignment specifier that has no effect or does not alter the alignment requirements change type compatibility? See also https://github.com/cplusplus/CWG/issues/867.


Comment from Issues list maintainer on 2026-11-26:

This issue was discussed at the November 2026 (Búzios) meeting of WG14. It was clear that alignment should be required to be consistent for common initial subsequences, and less clear exactly what "equivalent alignment specifier" should mean. Joseph Myers took an action item to propose wording for this issue.


Comment from Issues list maintainer on 2026-12-02:

In reflector message 38707, Joseph Myers proposed the following wording that would address both this issue and issue 1044.

Suggested correction (C23)

Insert new Constraints paragraphs in 6.7.6 (Alignment specifier), after the paragraph describing valid constant expressions in an alignment specifier.

An alignment specifier is said to be effective if it specifies nonzero alignment.

The sets (possibly empty) of alignment specifiers for two declarations or member declarations are said to be equivalent if:

Two declarations are said to have consistent sets of alignment specifiers if:

Replace the last paragraph of Semantics in 6.7.6 (Alignment specifier):

If the definition of an object has an alignment specifier, any other declaration of that object shall either specify equivalent alignment or have no alignment specifier. If the definition of an object does not have an alignment specifier, any other declaration of that object shall also have no alignment specifier. If declarations of an object in different translation units have different alignment specifiers, the behavior is undefined.

All declarations of the same object shall have consistent sets of alignment specifiers.

Modify a bullet point in 6.2.7 (Compatible type and composite type):

Modify the definition of "common initial sequence" in 6.5.3.4 (Structure and union members):

Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths), and equivalent sets of alignment specifiers, for a sequence of one or more initial members.

Modify the rules for tentative definitions in 6.9.3 (External object definitions):

If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier with an empty initializer, with equivalent alignment specifiers to the tentative definitions, and a type determined as follows:

Suggested correction (C2Y, N3998 draft)

Insert new Constraints paragraphs in 6.7.6 (Alignment specifier), after the paragraph describing valid constant expressions in an alignment specifier, the same as for C23 (see above).

In addition to the Constraints changes for C23, replace the last paragraph of Constraints in 6.7.6 (Alignment specifier):

If the definition of an object has an alignment specifier, any other declaration of that object in the same translation unit shall either specify equivalent alignment or have no alignment specifier. If the definition of an object does not have an alignment specifier, any other declaration of that object in the same translation unit shall also have no alignment specifier.

If an object is declared in a scope where a prior declaration of that object is visible, the two declarations shall have consistent sets of alignment specifiers.

Replace the last paragraph of Semantics in 6.7.6 (Alignment specifier):

If the definition of an object has an alignment specifier, no declaration of the object in a different translation unit shall specify a stricter alignment.

All declarations of the same object shall have consistent sets of alignment specifiers.

The changes to 6.2.7 (Compatible type and composite type), 6.5.3.4 (Structure and union members) and 6.9.3 (External object definitions) are the same as for C23, so are not repeated here.