空间配置器是STL的内存管理核心组件,负责封装内存分配和释放操作
空间配置器是STL中负责内存管理的组件,封装了内存分配和释放的细节。STL容器默认使用std::allocator
,但开发者可以自定义分配器以满足特定需求。
#include <vector> #include <memory> // 包含allocator int main() { // 使用默认分配器的vector std::vector<int, std::allocator<int>> numbers; numbers.push_back(10); numbers.push_back(20); numbers.push_back(30); // 显式使用分配器 std::allocator<int> alloc; int* p = alloc.allocate(1); alloc.construct(p, 100); std::cout << *p << std::endl; // 100 alloc.destroy(p); alloc.deallocate(p, 1); return 0; }
空间配置器提供以下核心功能:
分配足够存储n个对象的内存块
释放之前分配的内存块
在给定地址构造对象(C++17后弃用)
销毁给定地址的对象(C++17后弃用)
在C++17及更高版本中,construct
和destroy
成员函数已被弃用,取而代之的是std::allocator_traits
和通用构造/销毁工具。
自定义分配器在特定场景下非常有用:
减少内存分配开销,提高性能
在进程间共享数据结构
针对特定硬件的内存分配
内存泄漏检测和调试
下面是一个简单内存池分配器的实现示例:
#include <iostream> #include <vector> #include <memory> template <typename T> class MemoryPoolAllocator { public: using value_type = T; MemoryPoolAllocator() noexcept = default; template <typename U> MemoryPoolAllocator(const MemoryPoolAllocator<U>&) noexcept {} T* allocate(std::size_t n) { std::cout << "Allocating " << n << " object(s)\n"; return static_cast<T*>(::operator new(n * sizeof(T))); } void deallocate(T* p, std::size_t n) { std::cout << "Deallocating " << n << " object(s)\n"; ::operator delete(p); } // 可选:实现构造和销毁(C++17后通常不需要) template <typename U, typename... Args> void construct(U* p, Args&&... args) { std::cout << "Constructing object\n"; new (p) U(std::forward<Args>(args)...); } template <typename U> void destroy(U* p) { std::cout << "Destroying object\n"; p->~U(); } }; int main() { // 使用自定义分配器的vector std::vector<int, MemoryPoolAllocator<int>> vec; std::cout << "添加元素...\n"; vec.push_back(10); vec.push_back(20); vec.push_back(30); std::cout << "Vector内容: "; for (int n : vec) { std::cout << n << " "; } std::cout << "\n"; std::cout << "离开作用域,自动销毁...\n"; return 0; }
添加元素... Allocating 1 object(s) Constructing object Allocating 2 object(s) Constructing object Constructing object Deallocating 1 object(s) Allocating 4 object(s) Constructing object Constructing object Constructing object Deallocating 2 object(s) Vector内容: 10 20 30 离开作用域,自动销毁... Destroying object Destroying object Destroying object Deallocating 4 object(s)
C++17引入了多态分配器,允许在运行时切换分配策略:
#include <memory_resource> #include <vector> #include <iostream> int main() { // 创建内存池资源 std::pmr::unsynchronized_pool_resource pool; // 使用内存池的vector std::pmr::vector<int> vec{&pool}; vec.push_back(10); vec.push_back(20); vec.push_back(30); std::cout << "Vector内容: "; for (int n : vec) std::cout << n << " "; // 创建单调缓冲区资源 char buffer[1024]; std::pmr::monotonic_buffer_resource mono_res{buffer, sizeof(buffer)}; // 使用单调缓冲区的vector std::pmr::vector<int> temp_vec{&mono_res}; temp_vec.push_back(100); temp_vec.push_back(200); return 0; }