Pregunta

todos,

Si usa la biblioteca Boost Pool, ¿cómo reemplazaría la siguiente declaración:

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

Si fuera por un elemento, lo haría:

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

Pero dado que "numOfelements" es una variable, ¿tengo la sensación de ejecutar un bucle de malloc () no es una buena idea?

¿Fue útil?

Solución

Yo diría que necesitas usar pool_alloc interfaz:

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

Muestra de 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top