Site icon Learn C++

What Is aligned_alloc In Modern C++?

What Is aligned alloc In Modern C++

The C++11 standard introduced a new alignment feature as one of the many features of the C++ programming language that can be used with the newest C++ compilers today. This new feature was a new keyword align std::aligned_alloc that is used to provide a nested type that can be used as an uninitialized storage for any object whose size is at most the given object size by the alignment size requirement. In this post, we explain how we can use aligned_alloc in Modern C++.

What is alignment support in modern C++?

When we talk about ‘alignment’ in C++ it means a set of hints or instructions that tell the compiler to place the physical representation of data structures and variables in memory so that they line up at specific intervals of bytes – the underlaying digital representation of all data items. Alignment tells the compiler and linker to add additional ‘space’ to a value in memory so that the next object begins ‘nicely’ on a particular memory boundary.

Due to the way the CPU and other logic chips in a computer work, alignment can help create more computationally efficient programs. Think of it in the way you cut a cake – if you use an even number of slices for your cake it’s much easier and quicker to divide it up because you can cut the cake into nice even chunks of two. If you get an odd number of cake slices, it’s a lot harder to work out how big those slices need to be to make sure you don’t have a huge slice of cake left over – or someone gets a tiny slice (or none) and stays hungry! By cutting the cake in half each time and then each halved slice in half you are ‘aligning’ your cake slices evenly and optimally so all cake is efficiently used with no wasted cake.

The C++11 standard intends to extend the standard language and library with alignment-related features, known as alignment support. These alignment features include:

Alignment support in C++ can be found in more detail here in the C++ standards [Note: PDF link]. We also discuss the alignof keyword in this blog post: https://learncplusplus.org/what-is-the-alignof-expression-in-modern-c/.

In this post, we will explain the aligned_alloc which comes with C++17.

What is the aligned_alloc in modern C++?

The aligned_alloc (std::aligned_alloc) is defined in header <cstdlib> and used to allocate uninitialized storage in memory in bytes that alignment size is specified by alignment. The size parameter must be an integral multiple of alignment. Here are the syntax and helper type for the aligned_storage, according to open-std.org:

“The C++ standard now refers normatively to C11 (ISO/IEC 9899:2011) as “The C Standard”. Not only does ISO require that references to other international standards refer to the latest published version, and not to a historic version, but this also gives us access to aligned_alloc, which is useful for the improvements to our dynamic memory management.”

Syntax (Since C++17)

[crayon-6763570056556830433841/]

Is there an example to the aligned_alloc in modern C++?

Here is a simple example about aligned_alloc,

[crayon-676357005655e905077481/]

This allocates 512 bytes in the memory and aligns ‘double *x’ in 16 bytes.

Is there a full example to the aligned_alloc in modern C++?

[crayon-6763570056560413719835/]

Why aligned_alloc doesnt work in my C++?

Some Windows compilers do not support aligned_alloc because of some memory free and checking problems.

Note that, in some compilers, the some parts of the C11 Standard Library that are required by C++17, with the exception of C11 aligned_alloc(). The latter is unlikely to be implemented, because C11 specified aligned_alloc() in a way that’s incompatible with the Microsoft implementation of free(): namely, that free() must be able to handle highly aligned allocations. https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019

Thus, In this case, you may try to use _aligned_malloc

[crayon-6763570056561171482940/]

If you need more in-depth details about this feature, see the original Alignment Support Proposal document.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

Exit mobile version