Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::pool to manage the memory allocations in a std::vector

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.

like image 585
Verwirrt Avatar asked Jul 22 '14 16:07

Verwirrt


1 Answers

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.

like image 67
Wojtek Surowka Avatar answered Sep 19 '22 18:09

Wojtek Surowka