Questions tagged [allocator]

A component of C++'s Standard Library, in charge of handling the requests for allocation and deallocation of memory for a container.

Allocators are an important component of the C++ Standard Library. The standard library provides several containers which can change size during the execution of the program. To achieve this, some form of dynamic memory allocation is usually required. Allocators handle all the requests for allocation and deallocation of memory for a given container, and are also used to call constructors and destructors of the container elements.

730 questions
211
votes
17 answers

Compelling examples of custom C++ allocators?

What are some really good reasons to ditch std::allocator in favor of a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom…
Naaff
  • 9,213
  • 3
  • 38
  • 43
175
votes
3 answers

polymorphic_allocator: when and why should I use it?

Here is the documentation on cppreference, here is the working draft. I must admit that I didn't understand what's the real purpose of polymorphic_allocator and when/why/how I should use it. As an example, the pmr::vector has the following…
skypjack
  • 49,335
  • 19
  • 95
  • 187
60
votes
8 answers

How is a vector's data aligned?

If I want to process data in a std::vector with SSE, I need 16 byte alignment. How can I achieve that? Do I need to write my own allocator? Or does the default allocator already align to 16 byte boundaries?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
59
votes
3 answers

Does std::vector *have* to move objects when growing capacity? Or, can allocators "reallocate"?

A different question inspired the following thought: Does std::vector have to move all the elements when it increases its capacity? As far as I understand, the standard behaviour is for the underlying allocator to request an entire chunk of the…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
59
votes
2 answers

Why are are std::allocator's construct and destroy functions deprecated in c++17?

The c++17 specification deprecates the construct and destroy members of the std::allocator object. The working group provided rationale for deprecating other member functions here, under the heading "Deprecate the redundant members of…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
59
votes
4 answers

Making std::vector allocate aligned memory

Is it possible to make std::vector of custom structs allocate aligned memory for further processing with SIMD instructions? If it is possible to do with Allocator, does anyone happen to have such an allocator he could share?
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
46
votes
6 answers

Stack-buffer based STL allocator?

I was wondering if it practicable to have an C++ standard library compliant allocator that uses a (fixed sized) buffer that lives on the stack. Somehow, it seems this question has not been ask this way yet on SO, although it may have been implicitly…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
38
votes
3 answers

How can I write a stateful allocator in C++11, given requirements on copy construction?

As far as I can tell, the requirements on an allocator to be used with STL containers are laid out in Table 28 of section 17.6.3.5 of the C++11 standard. I'm a bit confused about the interaction between some of these requirements. Given a type X…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
38
votes
7 answers

Can a size_type ever be larger than std::size_t?

Standard containers with an std::allocator have their size_type defined as std::size_t. However, is it possible to have an allocator that allocates objects whose size cannot be represented by a size_t? In other words, can a size_type ever be larger…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
36
votes
2 answers

What is the purpose of std::scoped_allocator_adaptor?

In the C++11 standard we have std::scoped_allocator_adaptor in the dynamic memory management library. What are the most important use cases of this class?
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
35
votes
2 answers

C++ STL allocator vs operator new

According to C++ Primer 4th edition, page 755, there is a note saying: Modern C++ programs ordinarily ought to use the allocator class to allocate memory. It is safer and more flexible. I don't quite understand this statement. So far all the…
Ivan Xiao
  • 1,919
  • 3
  • 19
  • 30
35
votes
1 answer

Questions about Hinnant's stack allocator

I've been using Howard Hinnant's stack allocator and it works like a charm, but some details of the implementation are a little unclear to me. Why are global operators new and delete used? The allocate() and deallocate() member functions use…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
34
votes
4 answers

Why is allocator::rebind necessary when we have template template parameters?

Every allocator class must have an interface similar to the following: template class allocator { ... template struct rebind { typedef allocator other; }; }; And classes that use allocators do something…
user541686
  • 205,094
  • 128
  • 528
  • 886
34
votes
1 answer

Custom allocator in std::vector

Is it possible to use custom allocator for std::vector internal allocations? If yes, how?
Cartesius00
  • 23,584
  • 43
  • 124
  • 195
33
votes
1 answer

Should new C++ code use memory resources instead of allocators?

C++17 will bring us std::pmr::memory_resource which is a clean interface for allocating and deallocating memory. Unlike the Allocator concept, it does just that and nothing more. There will also be std::pmr::polymorphic_allocator which wraps a…
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
1
2 3
48 49