Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does allocator mean in STL

Tags:

c++

I'm using list class in c++ and i don't know what does allocator mean here

template < class T, class Allocator = allocator<T> > class list;

if i have list <int> mylist would it mean allocate integer type of memory using the allocator when an element is added to list? and when do you want a custom allocator?

like image 464
new Avatar asked Jul 25 '10 22:07

new


People also ask

What is the meaning of allocator?

Definitions of allocator. a person with authority to allot or deal out or apportion. synonyms: distributor. type of: authority. (usually plural) persons who exercise (administrative) control over others.

What is an allocator in programming?

Allocators handle all the requests for allocation and deallocation of memory for a given container. The C++ Standard Library provides general-purpose allocators that are used by default, however, custom allocators may also be supplied by the programmer.

What does allocator mean in C++?

Allocators are used by the C++ Standard Library to handle the allocation and deallocation of elements stored in containers. All C++ Standard Library containers except std::array have a template parameter of type allocator<Type> , where Type represents the type of the container element.

How does an allocator work?

A Merchandise Allocator allocates merchandise to stores according to warehouse and store inventory levels. Will assist stores with merchandise transfers to fulfill stock needs. Being a Merchandise Allocator may require a bachelor's degree.


2 Answers

Yes. An allocator is a way of factoring the allocation of memory from the use of memory. If a container needs some memory, instead of:

// too rigid, cannot allow custom allocation schemes
void* mem = ::operator new(someAmount);

You get:

// flexible, allows custom allocation schemes
void* mem = myallocator.allocate(someAmount);

There is a standard allocator, std::allocator, which uses global operator new and operator delete.

You want to use your own allocator anytime you need to allocate in a special way. These cases may be: get memory from some freelist, allocate from the stack, etc. (Generally for optimization purposes, though you could also record statistics with a custom allocator) Most of the time, the standard allocator works perfectly.

like image 89
GManNickG Avatar answered Nov 15 '22 17:11

GManNickG


In addition to what GMan pointed out, custom allocators can be used for aligned memory allocation. Some high performance libraries which use SIMD instruction set require aligned memory. Those libraries may provide you with ready to use allocators which you can plug into any STL container. There are also cache aligned allocators to avoid false sharing in multi-threaded code. Intel's TBB comes with scalable allocators, which can speed up multi-threaded memory allocation/delocation.

like image 45
user401947 Avatar answered Nov 15 '22 18:11

user401947