#include "cautomemorypool.h" #includeint main() { // create a new memory pool CAutoMemoryPool pool(1024); // allocate some memory from the pool int* data = pool.Alloc (10); // do something with the memory for (int i = 0; i < 10; i++) data[i] = i; // memory is automatically freed when 'pool' goes out of scope return 0; }
#include "cautomemorypool.h" #includeThese examples demonstrate how to use CAutoMemoryPool to allocate and free memory automatically. CAutoMemoryPool is included in the package library 'mempool'.class MyAllocator : public CAutoMemoryPoolAllocator { public: virtual void* Allocate(size_t size) override { // do something special here return CAutoMemoryPoolAllocator::Allocate(size); } }; int main() { // create a new memory pool with a custom allocator MyAllocator allocator; CAutoMemoryPool pool(1024, &allocator); // allocate some memory from the pool int* data = pool.Alloc (10); // do something with the memory for (int i = 0; i < 10; i++) data[i] = i; // memory is automatically freed when 'pool' goes out of scope return 0; }