I want to have a std::vector
of objects, with the objects allocated using boost::pool
. Is something like this correct:
class MyClass
{
private:
double data;
public:
MyClass(double d) : data(d) { }
};
int main()
{
std::vector<MyClass, boost::fast_pool_allocator<MyClass> > vect;
vect.push_back(4.5);
vect.push_back(9.8); //Are these being stored in a pool now?
return 0;
}
This code works, but I'm not entirely sure why. I am quite new to the concept of allocators, but if I understand correctly this is telling std::vector
to use a pool instead of the default allocator, so any elements created in the vector will be created from a pool.
What I'm not exactly sure of, is:
Where is the pool?
And how would I access the pool directly (to free memory for example)?
Does fast_pool_allocator
contain a pool, or do I need to create the pool separately and somehow tell the allocator to use it.
In the case of boost::fast_pool_allocator
the pool is a singleton owned by the allocator implementation. So you do not need to create anything separately.
You can access the allocator via get_allocator
function of std::vector
, or you can use static functions in boost::fast_pool_allocator
.
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