C++中std::allocator的使用案例详解
标准库中包含一个名为allocator的类允许我们将分配和初始化分离。使用allocator通常会提供更好的性能和更灵活的内存管理能力。new有一些灵活性上的局限其中一方面表现在它将内存分配和对象构造组合在了一起。类似的delete将对象析构和内存释放组合在了一起。我们分配单个对象时通常希望将内存分配和对象初始化组合在一起。因为在这种情况下我们几乎肯定知道对象应有什么值。当分配一大块内存时我们通常计划在这块内存上按需构造对象。在此情况下我们希望将内存分配和对象构造分离。这意味着我们可以分配大块内存但只在真正需要时才真正执行对象的创建操作(同时付出一定开销)。一般情况下将内存分配和对象构造组合在一起可能会导致不必要的浪费。标准库allocator类定义在头文件memory中它帮助我们将内存分配和对象构造分离开来。它提供一种类型感知的内存分配方法它分配的内存是原始的、未构造的。类似vectorallocator是一个模板。为了定义一个allocator对象我们必须指明这个allocator可以分配的对象类型。当一个allocator对象分配内存时它会根据给定的对象类型来确定恰当的内存大小和对齐位置。allocator支持的操作如下allocatro分配的内存是未构造的(unconstructed)。我们按需要在此内存中构造对象。在新标准库中construct成员函数接受一个指针和零个或多个额外参数在给定位置构造一个元素。额外参数用来初始化构造的对象。类似make_shared的参数这些额外参数必须是与构造的对象的类型相匹配的合法的初始化器。在早期版本的标准库中construct只接受两个参数指向创建对象位置的指针和一个元素类型的值。因此我们只能将一个元素拷贝到未构造空间中而不能用元素类型的任何其它构造函数来构造一个元素。还未构造对象的情况下就使用原始内存是错误的。为了使用allocator返回的内存我们必须用construct构造对象。使用未构造的内存其行为是未定义的。当我们用完对象后必须对每个构造的元素调用destroy来销毁它们。函数destroy接受一个指针对执行的对象执行析构函数。我们只能对真正构造了的元素进行destroy操作。一旦元素被销毁后就可以重新使用这部分内存来保存其它string也可以将其归还给系统。释放内存通过调用deallocate来完成。我们传递给deallocate的指针不能为空它必须指向由allocate分配的内存。而且传递给deallocate的大小参数必须与调用allocate分配内存时提供的大小参数具有一样的值。标准库还为allocator类定义了两个伴随算法可以在未初始化内存中创建对象。它们都定义在头文件memory中如下在C中内存是通过new表达式分配通过delete表达式释放的。标准库还定义了一个allocator类来分配动态内存块。分配动态内存的程序应负责释放它所分配的内存。内存的正确释放是非常容易出错的地方要么内存永远不会被释放要么在仍有指针引用它时就被释放了。新的标准库定义了智能指针类型------shared_ptr、unique_ptr和weak_ptr可令动态内存管理更为安全。对于一块内存当没有任何用户使用它时智能指针会自动释放它。现代C程序应尽可能使用智能指针。std::allocator是标准库容器的默认内存分配器。你可以替换自己的分配器这允许你控制标准容器分配内存的方式。以上内容主要摘自《CPrimer(Fifth Edition 中文版)》第12.2.2章节下面是从其他文章中copy的测试代码详细内容介绍可以参考对应的reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138#include allocator.hpp#include iostream#include memory#include string#include vectornamespaceallocator_ {// reference: C Primer(Fifth Edition) 12.2.2inttest_allocator_1(){std::allocatorstd::string alloc;// 可以分配string的allocator对象intn{ 5 };autoconstp alloc.allocate(n);// 分配n个未初始化的stringauto q p;// q指向最后构造的元素之后的位置alloc.construct(q);// *q为空字符串alloc.construct(q, 10,c);// *q为ccccccccccalloc.construct(q,hi);// *q为histd::cout *p std::endl;// 正确使用string的输出运算符//std::cout *q std::endl; // 灾难q指向未构造的内存std::cout p[0] std::endl;std::cout p[1] std::endl;std::cout p[2] std::endl;while(q ! p) {alloc.destroy(--q);// 释放我们真正构造的string}alloc.deallocate(p, n);return0;}inttest_allocator_2(){std::vectorint vi{ 1, 2, 3, 4, 5 };// 分配比vi中元素所占用空间大一倍的动态内存std::allocatorint alloc;auto p alloc.allocate(vi.size() * 2);// 通过拷贝vi中的元素来构造从p开始的元素/* 类似拷贝算法uninitialized_copy接受三个迭代器参数。前两个表示输入序列第三个表示这些元素将要拷贝到的目的空间。传递给uninitialized_copy的目的位置迭代器必须指向未构造的内存。与copy不同uninitialized_copy在给定目的位置构造元素。类似copyuninitialized_copy返回(递增后的)目的位置迭代器。因此一次uninitialized_copy调用会返回一个指针指向最后一个构造的元素之后的位置。*/auto q std::uninitialized_copy(vi.begin(), vi.end(), p);// 将剩余元素初始化为42std::uninitialized_fill_n(q, vi.size(), 42);return0;}// reference: http://www.modernescpp.com/index.php/memory-management-with-std-allocatorinttest_allocator_3(){std::cout std::endl;std::allocatorint intAlloc;std::cout intAlloc.max_size(): intAlloc.max_size() std::endl;int* intArray intAlloc.allocate(100);std::cout intArray[4]: intArray[4] std::endl;intArray[4] 2011;std::cout intArray[4]: intArray[4] std::endl;intAlloc.deallocate(intArray, 100);std::cout std::endl;std::allocatordouble doubleAlloc;std::cout doubleAlloc.max_size(): doubleAlloc.max_size() std::endl;std::cout std::endl;std::allocatorstd::string stringAlloc;std::cout stringAlloc.max_size(): stringAlloc.max_size() std::endl;std::string* myString stringAlloc.allocate(3);stringAlloc.construct(myString,Hello);stringAlloc.construct(myString 1,World);stringAlloc.construct(myString 2,!);std::cout myString[0] myString[1] myString[2] std::endl;stringAlloc.destroy(myString);stringAlloc.destroy(myString 1);stringAlloc.destroy(myString 2);stringAlloc.deallocate(myString, 3);std::cout std::endl;return0;}//// reference: http://en.cppreference.com/w/cpp/memory/allocatorinttest_allocator_4(){std::allocatorint a1;// default allocator for intsint* a a1.allocate(1);// space for one inta1.construct(a, 7);// construct the intstd::cout a[0] \n;a1.deallocate(a, 1);// deallocate space for one int// default allocator for stringsstd::allocatorstd::string a2;// same, but obtained by rebinding from the type of a1decltype(a1)::rebindstd::string::other a2_1;// same, but obtained by rebinding from the type of a1 via allocator_traitsstd::allocator_traitsdecltype(a1)::rebind_allocstd::string a2_2;std::string* s a2.allocate(2);// space for 2 stringsa2.construct(s,foo);a2.construct(s 1,bar);std::cout s[0] s[1] \n;a2.destroy(s);a2.destroy(s 1);a2.deallocate(s, 2);return0;}}// namespace allocator_