自定义allocator可控制STL容器内存行为,需定义value_type、allocate、deallocate等成员,通过模板参数传入容器使用,如std::vector<int, MyAllocator<int>>,适用于内存池等高性能场景。

在C++中,STL容器(如std::vector、std::list等)支持自定义内存分配器(allocator),通过替换默认的std::allocator,可以控制对象的内存分配行为。这在需要高性能内存管理、内存池、调试内存泄漏或嵌入式系统中非常有用。
allocator的基本要求
要自定义一个符合STL标准的allocator,必须满足一定的接口规范。一个合法的allocator类需包含以下关键成员:
- value_type:被分配对象的类型
- pointer:指向value_type的指针
- const_pointer:常量指针
- reference:引用类型
- const_reference:常量引用
- size_type:无符号整数类型,表示大小
- difference_type:有符号整数类型,表示指针差值
- allocate(n):分配未初始化的内存,可容纳n个value_type对象
- deallocate(p, n):释放由allocate分配的内存
- construct(p, args...):在已分配内存p上构造对象
- destroy(p):析构p指向的对象
-
rebind
:允许allocator适配不同类型的容器节点(如list内部用_Node)
实现一个简单的自定义allocator
下面是一个使用::operator new和::operator delete的简单自定义allocator示例,功能与std::allocator类似,但可用于学习结构:
立即学习“C++免费学习笔记(深入)”;
template<typename T>
struct MyAllocator {
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
<pre class='brush:php;toolbar:false;'>template<typename U>
struct rebind {
using other = MyAllocator<U>;
};
MyAllocator() = default;
template<typename U>
MyAllocator(const MyAllocator<U>&) {}
pointer allocate(size_type n) {
return static_cast<pointer>(::operator new(n * sizeof(T)));
}
void deallocate(pointer p, size_type n) {
::operator delete(p);
}
template<typename U, typename... Args>
void construct(U* p, Args&&... args) {
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template<typename U>
void destroy(U* p) {
p->~U();
}
bool operator==(const MyAllocator&) const { return true; }
bool operator!=(const MyAllocator&) const { return false; }};
在STL容器中使用自定义allocator
将自定义allocator作为模板参数传入即可:
立即学习“C++免费学习笔记(深入)”;
std::vector<int, MyAllocator<int>> vec; vec.push_back(10); vec.push_back(20);
对于std::list、std::deque等也是一样:
std::list<double, MyAllocator<double>> lst; lst.emplace_back(3.14);
更实用的例子:内存池allocator
实际应用中,自定义allocator常用于实现内存池,避免频繁调用系统分配函数。例如:
- 预分配一大块内存,后续分配从中切分
- 减少堆碎片
- 提升性能(特别是小对象频繁分配/释放)
这类allocator需要维护自己的内存管理逻辑,比如使用链表管理空闲块。虽然实现较复杂,但能显著优化特定场景下的性能。
基本上就这些。只要满足STL的allocator概念,你可以自由控制内存行为。注意:C++17后部分容器对allocator的要求有所简化,但兼容旧标准时仍建议完整实现。自定义时务必保证allocate/deallocate和construct/destroy成对正确工作。











