// allocatorStudy.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <new> #include <memory> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { /* 内存分配器allocator<>的使用 1.allocator将内存的分配与初始化解耦 2.allocate()和deallocate()成员函数处理的是未初始化的内存,它们不构造和销毁对象 3.在使用从allocate()获得的内存前,你必须在这块内存上创建对象;在通过deallocate()归还内存前,你需要销毁那些对象 4.没有理由在常规代码中使用allocator */ allocator<int> aloctorVar; int* ptr = aloctorVar.allocate(1000); /* ::operator new(1000 * sizeof(int)); */ aloctorVar.construct(ptr, 100); /* ::new (ptr)(100); */ aloctorVar.destroy(ptr); /* ptr->~int();调用析构函数 */ aloctorVar.deallocate(ptr, 1000); /* ::operator delete(ptr); */ return 0; }