Book Image

Modern C++ Programming Cookbook - Second Edition

By : Marius Bancila
5 (1)
Book Image

Modern C++ Programming Cookbook - Second Edition

5 (1)
By: Marius Bancila

Overview of this book

C++ has come a long way to be one of the most widely used general-purpose languages that is fast, efficient, and high-performance at its core. The updated second edition of Modern C++ Programming Cookbook addresses the latest features of C++20, such as modules, concepts, coroutines, and the many additions to the standard library, including ranges and text formatting. The book is organized in the form of practical recipes covering a wide range of problems faced by modern developers. The book also delves into the details of all the core concepts in modern C++ programming, such as functions and classes, iterators and algorithms, streams and the file system, threading and concurrency, smart pointers and move semantics, and many others. It goes into the performance aspects of programming in depth, teaching developers how to write fast and lean code with the help of best practices. Furthermore, the book explores useful patterns and delves into the implementation of many idioms, including pimpl, named parameter, and attorney-client, teaching techniques such as avoiding repetition with the factory pattern. There is also a chapter dedicated to unit testing, where you are introduced to three of the most widely used libraries for C++: Boost.Test, Google Test, and Catch2. By the end of the book, you will be able to effectively leverage the features and techniques of C++11/14/17/20 programming to enhance the performance, scalability, and efficiency of your applications.
Table of Contents (16 chapters)
13
Bibliography
14
Other Books You May Enjoy
15
Index

Controlling and querying object alignment

C++11 provides standardized methods for specifying and querying the alignment requirements of a type (something that was previously possible only through compiler-specific methods). Controlling the alignment is important in order to boost performance on different processors and enable the use of some instructions that only work with data on particular alignments.

For example, Intel Streaming SIMD Extensions (SSE) and Intel SSE2, which are a set of processor instructions that can greatly increase performance when the same operations are to be applied on multiple data objects, require 16 bytes of alignment of data. On the other hand, for Intel Advanced Vector Extensions (or Intel AVX), which expands most integer processor commands to 256 bits, it is highly recommended to use 32 bytes alignment. This recipe explores the alignas specifier for controlling the alignment requirements and the alignof operator, which retrieves the alignment requirements of a type.

Getting ready

You should be familiar with what data alignment is and the way the compiler performs default data alignment. However, basic information about the latter is provided in the How it works... section.

How to do it...

  • To control the alignment of a type (both at the class level or data member level) or an object, use the alignas specifier:
    struct alignas(4) foo
    {
      char a;
      char b;
    };
    struct bar
    {
      alignas(2) char a;
      alignas(8) int  b;
    };
    alignas(8)   int a;
    alignas(256) long b[4];
    
  • To query the alignment of a type, use the alignof operator:
    auto align = alignof(foo);
    

How it works...

Processors do not access memory one byte at a time, but in larger chunks of powers of two (2, 4, 8, 16, 32, and so on). Owing to this, it is important that compilers align data in memory so that it can be easily accessed by the processor. Should this data be misaligned, the compiler has to do extra work to access data; it has to read multiple chunks of data, shift and discard unnecessary bytes, and combine the rest.

C++ compilers align variables based on the size of their data type. The standard only specifies the sizes of char, signed char, unsigned char, char8_t, and std::byte, which must be 1. It also requires that the size of short must be at least 16 bits, the size of long must be at least 32 bits, and that the size of long long must be at least 64 bits. It also requires that 1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long). Therefore, the size of most types are compiler-specific and may depend on the platform. Typically, these are 1 byte for bool and char, 2 bytes for short, 4 bytes for int, long, and float, 8 bytes for double and long long, and so on. When it comes to structures or unions, the alignment must match the size of the largest member in order to avoid performance issues. To exemplify this, let's consider the following data structures:

struct foo1    // size = 1, alignment = 1
{              // foo1:    +-+
  char a;      // members: |a|
};
struct foo2    // size = 2, alignment = 1
{              // foo2:    +-+-+
  char a;      // members  |a|b|
  char b;
};
struct foo3    // size = 8, alignment = 4
{              // foo3:    +----+----+
  char a;      // members: |a...|bbbb|
  int  b;      // . represents a byte of padding
};

foo1 and foo2 are different sizes, but the alignment is the same—that is, 1—because all data members are of the type char, which has a size of 1 byte. In the structure foo3, the second member is an integer, whose size is 4. As a result, the alignment of members of this structure is done at addresses that are multiples of 4. To achieve this, the compiler introduces padding bytes.

The structure foo3 is actually transformed into the following:

struct foo3_
{
  char a;        // 1 byte
  char _pad0[3]; // 3 bytes padding to put b on a 4-byte boundary
  int  b;        // 4 bytes
};

Similarly, the following structure has a size of 32 bytes and an alignment of 8; that is because the largest member is a double whose size is 8. This structure, however, requires padding in several places to make sure that all the members can be accessed at addresses that are multiples of 8:

struct foo4     // size = 24, alignment = 8
{               // foo4:    +--------+--------+--------+--------+
  int a;        // members: |aaaab...|cccc....|dddddddd|e.......|
  char b;       // . represents a byte of padding
  float c;
  double d;
  bool e;
};

The equivalent structure that's created by the compiler is as follows:

struct foo4_
{
  int a;         // 4 bytes
  char b;        // 1 byte
  char _pad0[3]; // 3 bytes padding to put c on a 8-byte boundary
  float c;       // 4 bytes
  char _pad1[4]; // 4 bytes padding to put d on a 8-byte boundary
  double d;      // 8 bytes
  bool e;        // 1 byte
  char _pad2[7]; // 7 bytes padding to make sizeof struct multiple of 8
};

In C++11, specifying the alignment of an object or type is done using the alignas specifier. This can take either an expression (an integral constant expression that evaluates to 0 or a valid value for an alignment), a type-id, or a parameter pack. The alignas specifier can be applied to the declaration of a variable or a class data member that does not represent a bit field, or to the declaration of a class, union, or enumeration.

The type or object on which an alignas specification is applied will have the alignment requirement equal to the largest, greater than zero, expression of all alignas specifications used in the declaration.

There are several restrictions when using the alignas specifier:

  • The only valid alignments are the powers of two (1, 2, 4, 8, 16, 32, and so on). Any other values are illegal, and the program is considered ill-formed; that doesn't necessarily have to produce an error, as the compiler may choose to ignore the specification.
  • An alignment of 0 is always ignored.
  • If the largest alignas on a declaration is smaller than the natural alignment without any alignas specifier, then the program is also considered ill-formed.

In the following example, the alignas specifier has been applied to a class declaration. The natural alignment without the alignas specifier would have been 1, but with alignas(4), it becomes 4:

struct alignas(4) foo
{
  char a;
  char b;
};

In other words, the compiler transforms the preceding class into the following:

struct foo
{
  char a;
  char b;
  char _pad0[2];
};

The alignas specifier can be applied both to the class declaration and the member data declarations. In this case, the strictest (that is, largest) value wins. In the following example, member a has a natural size of 1 and requires an alignment of 2; member b has a natural size of 4 and requires an alignment of 8, so the strictest alignment would be 8. The alignment requirement of the entire class is 4, which is weaker (that is, smaller) than the strictest required alignment and therefore it will be ignored, though the compiler will produce a warning:

struct alignas(4) foo
{
  alignas(2) char a;
  alignas(8) int  b;
};

The result is a structure that looks like this:

struct foo
{
  char a;
  char _pad0[7];
  int b;
  char _pad1[4];
};

The alignas specifier can also be applied to variables. In the following example, variable a, which is an integer, is required to be placed in memory at a multiple of 8. The next variable, the array of 4 longs, is required to be placed in memory at a multiple of 256. As a result, the compiler will introduce up to 244 bytes of padding between the two variables (depending on where in memory, at an address multiple of 8, variable a is located):

alignas(8)   int a;
alignas(256) long b[4];
printf("%p\n", &a); // eg. 0000006C0D9EF908
printf("%p\n", &b); // eg. 0000006C0D9EFA00

Looking at the addresses, we can see that the address of a is indeed a multiple of 8, and that the address of b is a multiple of 256 (hexadecimal 100).

To query the alignment of a type, we use the alignof operator. Unlike sizeof, this operator can only be applied to type-ids, and not to variables or class data members. The types it can be applied to can be complete types, an array type, or a reference type. For arrays, the value that's returned is the alignment of the element type; for references, the value that's returned is the alignment of the referenced type. Here are several examples:

Expression

Evaluation

alignof(char)

1, because the natural alignment of char is 1.

alignof(int)

4, because the natural alignment of int is 4.

alignof(int*)

4 on 32-bit, 8 on 64-bit, the alignment for pointers.

alignof(int[4])

4, because the natural alignment of the element type is 4.

alignof(foo&)

8, because the specified alignment for the class foo, which is the referred type (as shown in the previous example), was 8.

The alignas specifier is useful if you wish to force an alignment for a data type (taking into consideration the restriction mentioned previously) so that variables of that type can be accessed and copied efficiently. This means optimizing CPU reads and writes and avoiding unnecessary invalidation from cache lines. This can be highly important in some categories of applications where performance is key, such as games or trading applications. On the other hand, the alignof operator retries the minimum alignment requirement of a specified type.

See also

  • Creating type aliases and alias templates to learn about aliases for types