Files
2025-10-25 03:02:53 +03:00

154 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[basic.align]
# 6 Basics [[basic]](./#basic)
## 6.8 Memory and objects [[basic.memobj]](basic.memobj#basic.align)
### 6.8.3 Alignment [basic.align]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3727)
Object types have [*alignment requirements*](#def:alignment_requirement,implementation-defined "6.8.3Alignment[basic.align]") ([[basic.fundamental]](basic.fundamental "6.9.2Fundamental types"), [[basic.compound]](basic.compound "6.9.4Compound types"))
which place restrictions on the addresses at which an object of that type
may be allocated[.](#1.sentence-1)
An [*alignment*](#def:alignment "6.8.3Alignment[basic.align]") is an implementation-defined
integer value representing the number of bytes between successive addresses
at which a given object can be allocated[.](#1.sentence-2)
An object type imposes an alignment
requirement on every object of that type; stricter alignment can be requested
using the alignment specifier ([[dcl.align]](dcl.align "9.13.2Alignment specifier"))[.](#1.sentence-3)
Attempting to create an object ([[intro.object]](intro.object "6.8.2Object model")) in storage that
does not meet the alignment requirements of the object's type
is undefined behavior[.](#1.sentence-4)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3739)
A [*fundamental alignment*](#def:alignment,fundamental "6.8.3Alignment[basic.align]") 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]](support.types "17.2Common definitions"))[.](#2.sentence-1)
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[.](#2.sentence-2)
[*Example [1](#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[.](#2.sentence-3)
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[.](#2.sentence-4)
— *end example*]
The result of the alignof operator reflects the alignment
requirement of the type in the complete-object case[.](#2.sentence-5)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3761)
An [*extended alignment*](#def:alignment,extended "6.8.3Alignment[basic.align]") is represented by an alignment
greater than alignof(std::max_align_t)[.](#3.sentence-1)
It is implementation-defined
whether any extended alignments are supported and the contexts in which they are
supported ([[dcl.align]](dcl.align "9.13.2Alignment specifier"))[.](#3.sentence-2)
A type having an extended alignment
requirement is an [*over-aligned type*](#def:type,over-aligned "6.8.3Alignment[basic.align]")[.](#3.sentence-3)
[*Note [1](#note-1)*:
Every over-aligned type is or contains a class type
to which extended alignment applies (possibly through a non-static data member)[.](#3.sentence-4)
— *end note*]
A [*new-extended alignment*](#def:alignment,new-extended "6.8.3Alignment[basic.align]") is represented by
an alignment greater than __STDCPP_DEFAULT_NEW_ALIGNMENT__ ([[cpp.predefined]](cpp.predefined "15.12Predefined macro names"))[.](#3.sentence-5)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3774)
Alignments are represented as values of the type std::size_t[.](#4.sentence-1)
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[.](#4.sentence-2)
Every alignment value shall be a non-negative integral power of two[.](#4.sentence-3)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3782)
Alignments have an order from [*weaker*](#def:alignment,weaker "6.8.3Alignment[basic.align]") to[*stronger*](#def:alignment,stronger "6.8.3Alignment[basic.align]") or [*stricter*](#def:alignment,stricter "6.8.3Alignment[basic.align]") alignments[.](#5.sentence-1)
Stricter
alignments have larger alignment values[.](#5.sentence-2)
An address that satisfies an alignment
requirement also satisfies any weaker valid alignment requirement[.](#5.sentence-3)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3788)
The alignment requirement of a complete type can be queried using analignof expression ([[expr.alignof]](expr.alignof "7.6.2.6Alignof"))[.](#6.sentence-1)
Furthermore,
the narrow character types ([[basic.fundamental]](basic.fundamental "6.9.2Fundamental types")) shall have the weakest
alignment requirement[.](#6.sentence-2)
[*Note [2](#note-2)*:
This enables the ordinary character types to be used as the
underlying type for an aligned memory area ([[dcl.align]](dcl.align "9.13.2Alignment specifier"))[.](#6.sentence-3)
— *end note*]
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3798)
Comparing alignments is meaningful and provides the obvious results:
- [(7.1)](#7.1)
Two alignments are equal when their numeric values are equal[.](#7.1.sentence-1)
- [(7.2)](#7.2)
Two alignments are different when their numeric values are not equal[.](#7.2.sentence-1)
- [(7.3)](#7.3)
When an alignment is larger than another it represents a stricter alignment[.](#7.3.sentence-1)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3807)
[*Note [3](#note-3)*:
The runtime pointer alignment function ([[ptr.align]](ptr.align "20.2.5Pointer alignment"))
can be used to obtain an aligned pointer within a buffer;
an [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ([[dcl.align]](dcl.align "9.13.2Alignment specifier"))
can be used to align storage explicitly[.](#8.sentence-1)
— *end note*]
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3815)
If a request for a specific extended alignment in a specific context is not
supported by an implementation, the program is ill-formed[.](#9.sentence-1)