3

Aim: To understand the motivation why C++17 introduced std::aligned_alloc for dynamic memory management.

Problem: For the memory allocation in C++, using std::malloc is virtually always discouraged due to various reasons exemplified under In what cases do I use malloc and/or new?. Instead, using new expression is almost always encouraged in low-level code (e.g. see Notes).

Despite this discouragement, I wonder why C++17 introduced std::aligned_alloc which looks like a continuation of std::malloc.

Is there anything in C++17 (and beyond) that cannot be performed by new (or other equivalent utilities encouraged to use, if any)?

Attempts: I could only find the following discussion, which is still remotely related to my question:

Difference between aligned malloc and standard malloc?

Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34

1 Answers1

7

C++17 bumped up its support for plain C from C99 to C11. It inherited aligned_alloc along with other stuff from C11.

paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0063r3.html

So the motivation is to make C features available in C++ for those who want them.