I've just read about std::allocator
. In my opinion, it is more complicated to use it instead of using new
and delete
.
With allocator
we must explicitly allocate heap memory, construct it, destroy it, and then finally deallocate the memory. So why was it created?
In which cases can it be used and when should it be used instead of new and delete?
Member functions associated with std::allocator() : address: It is used for obtaining the address of an object although it is removed in C++20. construct: It is used to construct an object.It is also removed in C++20. destroy: It is used to destruct an object in allocated storage.It is also removed in C++20.
Under the hood, the C function std::malloc will typically be used. Therefore, an allocator, who uses preallocated memory can gain a great performance boost. An adjusted allocator also makes a lot of sense, if you need a deterministic timing behavior of your program.
The std::vector<> is a container from the c++ Standard Template Library (STL) which has a number of features. It, however, fundamentally has the same basic underlying function as new[], which is allocating memory for a continuous sequence of object.
The default allocator uses new and delete to allocate and deallocate memory. If you want to use a different method of memory allocation, such as using shared memory, then you must create your own allocator. If you are targeting C++11 and you need to write a new custom allocator, make it a minimal allocator if possible.
In my opinion, it is more complicated to use it instead of using new and delete.
Yes, but it is not meant to replace new
and delete
, it serves a different purpose.
With allocator we must explicitly allocate heap memory, construct it, destroy it, and then finally deallocate the memory.
So why was it created?
Because sometimes you want to separate allocation and construction into two steps (and similarly to separate destruction and deallocation into two steps). If you don't want to do that, don't use an allocator, use new
instead.
In which cases can it be used and when should it be used instead of new and delete?
When you need the behaviour of an allocator, not the behaviour of new
and delete
, obviously! The typical case is when implementing a container.
Consider the following code:
std::vector<X> v; v.reserve(4); // (1) v.push_back( X{} ); // (2) v.push_back( X{} ); // (3) v.clear(); // (4)
Here line (1) must allocate enough memory for four objects, but not construct them yet. Then lines (2) and (3) must construct objects into the allocated memory. Then line (4) must destroy those objects, but not deallocate the memory. Finally, in the vector's destructor, all the memory can be deallocated.
So the vector cannot just use new X()
or delete &m_data[1]
to create and destroy the objects, it must perform allocation/deallocation separately from construction/destruction. A container's allocator template argument defines the policy that should be used for (de)allocating memory and constructing/destructing objects, allowing the container's use of memory to be customised. The default policy is the std::allocator
type.
So you use an allocator when an allocator is required (such as when using a container) and you use std::allocator
when you don't want to provide a custom allocator and just want the standard one.
You don't use an allocator as a replacement for new
and delete
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With