L
L
LNK2016-06-28 16:39:44
C++ / C#
LNK, 2016-06-28 16:39:44

What are allocators for and what do they do in C++?

After completing one course, I'm trying to understand what they are and what they are for, these allocators in C ++. Explain, please, their practical purpose and what they should do.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Zhilin, 2016-06-28
@NikHaker

The allocator is able to allocate and free memory in the required quantities in a certain way. std::allocator is an example implementation of an allocator from the standard library, just using new and delete, which normally access the malloc and free system calls.
A more complex example is the pool allocator. Since system calls are expensive, why not save on them? Let's allocate 1 gigabyte of memory at once (for example), and then in the allocator we will issue memory from this pool and increase the head pointer (again, simplified). There is only one real memory allocation, there are almost no system calls, hooray, the program has accelerated.
Actually, this is what allocators are for, to insert your own, cool, non-standard, memory allocation anywhere. Most standard containers accept them.

S
SolidMinus, 2016-06-28
@SolidMinus

There are a number of smart and understandable articles on Habré, for example this one:
Alternative memory allocators

E
evnuh, 2016-06-28
@evnuh

Very briefly - for performance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question