Question

all,

If you use boost pool library, how would you replace the following statement:

MyStruct *someStruct = (MyStruct *) calloc(numOfElements, sizeof(MyStruct));

If it was for one element, I would do:

boost::object_pool<MyStruct> myPool;
MyStruct *someStruct = myPool.malloc();

but since "numOfElements" is a variable, I have the feeling executing a loop of malloc() is not a good idea?

Was it helpful?

Solution

I'd say you need to use pool_alloc interface:

static pointer allocate(size_type n);
static pointer allocate(size_type n, pointer);
static void deallocate(pointer ptr, size_type n);

Sample from http://www.boost.org/doc/libs/1_47_0/libs/pool/doc/interfaces.html

void func()
{
    std::vector<int, boost::pool_allocator<int> > v;
    for (int i = 0; i < 10000; ++i)
        v.push_back(13);
} // Exiting the function does NOT free the system memory allocated by the pool allocator
  // You must call
  //  boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::release_memory()
  // in order to force that
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top