Frage

alle,

Wenn Sie die Boost -Pool -Bibliothek verwenden, wie würden Sie die folgende Anweisung ersetzen:

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

Wenn es für ein Element wäre, würde ich es tun:

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

Aber da "Numofelements" eine Variable ist, habe ich das Gefühl, eine Schleife Malloc () auszuführen, keine gute Idee?

War es hilfreich?

Lösung

Ich würde sagen, Sie müssen verwenden pool_alloc Schnittstelle:

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

Probe von 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top