Callocの代替プールをブーストします
-
25-10-2019 - |
質問
全て、
Boost Pool Libraryを使用する場合、次のステートメントをどのように置き換えますか。
MyStruct *someStruct = (MyStruct *) calloc(numOfElements, sizeof(MyStruct));
それが1つの要素のためであれば、私はします:
boost::object_pool<MyStruct> myPool;
MyStruct *someStruct = myPool.malloc();
しかし、「numofelements」は変数であるため、malloc()のループを実行するのは良い考えではありませんか?
解決
使用する必要があると思います pool_alloc
インターフェース:
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/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
所属していません StackOverflow