gpt4 book ai didi

c++ - 创建范围自定义内存池/分配器?

转载 作者:太空狗 更新时间:2023-10-29 20:31:41 25 4
gpt4 key购买 nike

是否可以在 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 库或其他任何地方都找不到这样的东西。

是否存在使这成为不可能的根本问题?

最佳答案

您需要创建一个自定义分配器,将其作为模板参数传递给 vector。这个自定义分配器基本上会包装对您的池的访问,并根据需要进行任何大小验证。

关于c++ - 创建范围自定义内存池/分配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3670828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com