Domanda

tutto,

Se si utilizza biblioteca piscina spinta, come è possibile sostituire la seguente dichiarazione:

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

Se fosse stato per un elemento, lo farei:

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

ma dal momento che "numOfElements" è una variabile, ho la sensazione di eseguire un ciclo di malloc () non è una buona idea?

È stato utile?

Soluzione

Direi che è necessario utilizzare pool_alloc interfaccia:

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

http: //www.boost. org / doc / librerie / 1_47_0 / librerie / piscina / 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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top