在C++中,是否可以创建一个自定义分配器,其工作方式仅如下:
{
// Limit memory to 1024 KB
ScopedMemoryPool memoryPool(1024 * 1024);
// From here on all heap allocations ('new', 'malloc', ...) take memory from the pool.
// If the pool is depleted these calls result in an exception being thrown.
// Examples:
std::vector<int> integers(10);
int a * = new int [10];
}
我在boost库或其他地方都找不到类似的东西。
这是否存在一些根本性的问题使得这种操作不可能?
boost::pool_allocator
和boost::fast_pool_allocator
可以用作std::vector
和其他容器的分配器。 - Mike Seymour