Files
cppdraft_translate/cppdraft/basic/align.md
2025-10-25 03:02:53 +03:00

6.6 KiB
Raw Blame History

[basic.align]

6 Basics [basic]

6.8 Memory and objects [basic.memobj]

6.8.3 Alignment [basic.align]

1

#

Object types have alignment requirements ([basic.fundamental], [basic.compound]) which place restrictions on the addresses at which an object of that type may be allocated.

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

An object type imposes an alignment requirement on every object of that type; stricter alignment can be requested using the alignment specifier ([dcl.align]).

Attempting to create an object ([intro.object]) in storage that does not meet the alignment requirements of the object's type is undefined behavior.

2

#

A fundamental alignment is represented by an alignment less than or equal to the greatest alignment supported by the implementation in all contexts, which is equal toalignof(std::max_align_t) ([support.types]).

The alignment required for a type may be different when it is used as the type of a complete object and when it is used as the type of a subobject.

[Example 1: struct B { long double d; };struct D : virtual B { char c; };

When D is the type of a complete object, it will have a subobject of type B, so it must be aligned appropriately for a long double.

If D appears as a subobject of another object that also has B as a virtual base class, the B subobject might be part of a different subobject, reducing the alignment requirements on the D subobject.

— end example]

The result of the alignof operator reflects the alignment requirement of the type in the complete-object case.

3

#

An extended alignment is represented by an alignment greater than alignof(std::max_align_t).

It is implementation-defined whether any extended alignments are supported and the contexts in which they are supported ([dcl.align]).

A type having an extended alignment requirement is an over-aligned type.

[Note 1:

Every over-aligned type is or contains a class type to which extended alignment applies (possibly through a non-static data member).

— end note]

A new-extended alignment is represented by an alignment greater than STDCPP_DEFAULT_NEW_ALIGNMENT ([cpp.predefined]).

4

#

Alignments are represented as values of the type std::size_t.

Valid alignments include only those values returned by an alignof expression for the fundamental types plus an additional implementation-defined set of values, which may be empty.

Every alignment value shall be a non-negative integral power of two.

5

#

Alignments have an order from weaker tostronger or stricter alignments.

Stricter alignments have larger alignment values.

An address that satisfies an alignment requirement also satisfies any weaker valid alignment requirement.

6

#

The alignment requirement of a complete type can be queried using analignof expression ([expr.alignof]).

Furthermore, the narrow character types ([basic.fundamental]) shall have the weakest alignment requirement.

[Note 2:

This enables the ordinary character types to be used as the underlying type for an aligned memory area ([dcl.align]).

— end note]

7

#

Comparing alignments is meaningful and provides the obvious results:

  • (7.1)

    Two alignments are equal when their numeric values are equal.

  • (7.2)

    Two alignments are different when their numeric values are not equal.

  • (7.3)

    When an alignment is larger than another it represents a stricter alignment.

8

#

[Note 3:

The runtime pointer alignment function ([ptr.align]) can be used to obtain an aligned pointer within a buffer; an alignment-specifier ([dcl.align]) can be used to align storage explicitly.

— end note]

9

#

If a request for a specific extended alignment in a specific context is not supported by an implementation, the program is ill-formed.