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. Glad it’s been working for you. 1. Why are global operators new and delete used? The allocate() and deallocate() member functions use ::operator new and ::operator delete respectively. Similarly, the … Read more

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

A quoted text from Foundations of Algorithms in C++11, Volume 1, chap 4, p. 35 : template <typename T> struct allocator { template <typename U> using rebind = allocator<U>; }; sample usage : allocator<int>::rebind<char> x; In The C++ Programming Language, 4th edition, section 34.4.1, p. 998, commenting the ‘classical’ rebind member in default allocator class … Read more

polymorphic_allocator: when and why should I use it?

Choice quote from cppreference: This runtime polymorphism allows objects using polymorphic_allocator to behave as if they used different allocator types at run time despite the identical static allocator type The issue with “regular” allocators is that they change the type of the container. If you want a vector with a specific allocator, you can make … Read more

How is a vector’s data aligned?

C++ standard requires allocation functions (malloc() and operator new()) to allocate memory suitably aligned for any standard type. As these functions don’t receive the alignment requirement as an argument, in practice it means that the alignment for all allocations is the same, and is that of a standard type with the largest alignment requirement, which … Read more

Stack-buffer based STL allocator?

It’s definitely possible to create a fully C++11/C++14 conforming stack allocator*. But you need to consider some of the ramifications about the implementation and the semantics of stack allocation and how they interact with standard containers. Here’s a fully C++11/C++14 conforming stack allocator (also hosted on my github): #include <functional> #include <memory> template <class T, … Read more