Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

With the latest trends about C and C++ applications, and with latest I mean the

ID: 658515 • Letter: W

Question

With the latest trends about C and C++ applications, and with latest I mean the latest years, I was expecting to see std::allocators to be used way more frequently than what it really is.

Modern applications are usually multithreaded or with form of concurrency in them, and they manage a significant amount of memory, especially in some fields like games and multimedia applications; so the usual high cost of the allocation/deallocations rises and impacts the performances even more than in old applications.

But for what I can see this kind of memory management through std::allocators is not popular, I can't even count 1 library that I know of, that is using any rationale about a standardize way of managing memory.

There is a real reason why std::allocator is not popular ? Technical reasons ?

Explanation / Answer

There are sort of two questions here:

1) Why isn't std::allocator used more?

The short answer is because for most uses, using new and delete is sufficient.

First, a brief overview of what the C++11 Allocator concept is intended for. Basically, when you do a new Foo, it both allocates memory and then constructs the object in that memory.

But what if you needed to allocate memory and construct as two separate steps. The classic example of this is std::vector, which can allocate memory without initializing it immediately, but then constructs the elements as you add them to the container. The point of std::allocator is to provide an interface for doing those separate steps in a uniform way.

However, for most users, they don't need that level of control, because it's just another thing to remember to do when cleaning up your exceptions.

2) Why don't more things make use of the C++ concept of Allocator?

Mostly the need isn't there. The only classes that tend to use them are the ones that want to give their users more control over the source of the memory they allocate. The Standard library containers are written that way so that the user can make those decisions if they choose, but they default to using the normal allocator.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote