Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
53
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 100: Alignment

All types in C++ have an alignment. This is a restriction on the memory address that objects of that type can be created within. A memory address is valid for an object's creation if dividing that address by the object's alignment is a whole number.

Type alignments are always a power of two (including 1).

Section 100.1: Controlling alignment

Version ≥ C++11

The alignas keyword can be used to force a variable, class data member, declaration or definition of a class, or declaration or definition of an enum, to have a particular alignment, if supported. It comes in two forms:

alignas(x), where x is a constant expression, gives the entity the alignment x, if supported.

alignas(T), where T is a type, gives the entity an alignment equal to the alignment requirement of T, that is, alignof(T), if supported.

If multiple alignas specifiers are applied to the same entity, the strictest one applies.

In this example, the bu er buf is guaranteed to be appropriately aligned to hold an int object, even though its element type is unsigned char, which may have a weaker alignment requirement.

alignas(int) unsigned char buf[sizeof(int)]; new (buf) int(42);

alignas cannot be used to give a type a smaller alignment than the type would have without this declaration:

alignas(1) int i; //Il-formed, unless `int` on this platform is aligned to 1 byte. alignas(char) int j; //Il-formed, unless `int` has the same or smaller alignment than `char`.

alignas, when given an integer constant expression, must be given a valid alignment. Valid alignments are always powers of two, and must be greater than zero. Compilers are required to support all valid alignments up to the alignment of the type std::max_align_t. They may support larger alignments than this, but support for allocating memory for such objects is limited. The upper limit on alignments is implementation dependent.

C++17 features direct support in operator new for allocating memory for over-aligned types.

Section 100.2: Querying the alignment of a type

Version ≥ c++11

The alignment requirement of a type can be queried using the alignof keyword as a unary operator. The result is a constant expression of type std::size_t, i.e., it can be evaluated at compile time.

#include <iostream> int main() {

std::cout << "The alignment requirement of int is: " << alignof(int) << '\n';

}

Possible output

GoalKicker.com – C++ Notes for Professionals

509

The alignment requirement of int is: 4

If applied to an array, it yields the alignment requirement of the element type. If applied to a reference type, it yields the alignment requirement of the referenced type. (References themselves have no alignment, since they are not objects.)

GoalKicker.com – C++ Notes for Professionals

510