C++C++17C++20Learn C++Syntax

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:

  • The alignment specifier alignas for declarations.
  • The alignof expression to retrieve alignment requirements of a type.
  • Alignment arithmetic by library support (aligned_storage, aligned_union).
  • std::align standard function for pointer alignment at run time.

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)

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

Here is a simple example about aligned_alloc,

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++?

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

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

What Is aligned alloc In Modern C++ The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?