草庐IT

Allocator

全部标签

c++ - 为什么容器分配器需要指定它们分配的类型?

如果我为容器使用自定义分配器,我不喜欢必须重复包含的类型名:templatestructMyAllocator:publicstd::allocator{//...Usualallocatorimplementation};typedefstd::vector>int_container;typedefstd::vector>int_container_wrong_allocator;根据标准,第二行是未定义的行为,尽管大多数实现会将分配器重新绑定(bind)到正确的类型。我的问题是,既然要求容器和分配器属于同一类型,为什么没有适当的标准机制来强制执行(或完全避免)并消除用户错误的可能

c++ - 如何将自定义分配器的完全相同状态传递给多个容器?

我正在编写一个分配器,它引用了某个类的另一个实例,该类跟踪分配的字节数。下面是我正在尝试做的事情的一个最小示例(改编自here),只是没有整个内存跟踪类,而是我引用了一些收集到目前为止分配的字节的int。此引用在main内部分配,应传递给CustomAllocator:#include//numeric_limits#include#include//typeid//container#include#include#includetemplateclassCustomAllocator{public://typedefinitionstypedefTvalue_type;/**Ele

c++ - std::allocate_shared,允许从自定义分配器和单次分配中分配共享指针引用计数

Makeshared使用引用计数和对象的单一分配提高性能,是否可以将自定义分配器与std::allocate_shared一起使用并且仍然有一个分配,根据我写的测试代码它没有发生:sample我知道boost::intrusive,但它有额外的代码需要编写并且可能出错 最佳答案 在一些调试的帮助下发现std::allocate_shared做了预期的事情,它允许你只为你的对象和引用计数器进行一次分配。下面是更正后的代码:allocateshared此处operatornew对std::make_shared和std::allocat

c++ - 我可以使用 STL 分配器将整个 STL 容器及其内容(键和值)存储在我创建的内存块中吗?

我试图了解我是否可以使用STL分配器(http://www.drdobbs.com/the-standard-librarian-what-are-allocato/184403759)或任何其他C++机制来达到我的目的。我自己分配了一个很大的内存块。我想创建一个像map这样的STL容器,这样容器,包括存储在容器中的键和值,都存储在这个内存块中。明确地说,当我说“容器,包括键和值”时,我指的是通常由容器内的代码在堆上分配的所有内存位。例如。图形结构,以及键和值的拷贝。map对象本身(不包括上述位)在我的内存块中的存储将由我处理。显然,我找不到其他东西来为我做这件事。我知道有些回复可能是

c++ - 如何使我的 uninitialised_allocator 安全?

来自thisquestion,我想用unitialised_allocator比如说,std::vector避免在构建时对元素进行默认初始化(或resize()的std::vector(有关用例,另请参阅here)。我当前的设计如下所示://basedonadesignbyJaredHoberocktemplatestructuninitialised_allocator:base_allocator::templaterebind::other{//addedbyWalterQ:ISTHISTHECORRECTCONDITION?static_assert(std::is_trivi

c++ - 如何防止在 boost::fast_pool_allocator 管理的对象上调用析构函数?

我想利用boost::fast_pool_allocator的以下广告功能(参见theBoostdocumentationforBoostPool):Forexample,youcouldhaveasituationwhereyouwanttoallocateabunchofsmallobjectsatonepoint,andthenreachapointinyourprogramwherenoneofthemareneededanymore.Usingpoolinterfaces,youcanchoosetoruntheirdestructorsorjustdropthemoffin

c++ - 为什么 std::allocator 要求 propagate_on_container_move_assignment 为真?

根据当前标准(20.7.9),std::allocator有一个成员propagate_on_container_move_assignment设置为true_type:templateclassallocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;templatestruc

c++ - 实现 std::vector::push_back 强异常安全

我正在根据2018年后的圣地亚哥草案(N4791)实现我自己的vector,并且有一些关于实现强异常安全性的问题。这是一些代码:templatevoidVector::push_back(constT&value){if(buffer_capacity==0){this->Allocate(this->GetSufficientCapacity(1));}if(buffer_sizeConstruct(value);return;}autonew_buffer=CreateNewBuffer(this->GetSufficientCapacity(buffer_size+1),allo

c++ - 处理自定义分配器中没有默认构造函数

我对我的new宏有一个#define,以使用我自己的分配器,例如MYNEW(Type,Allocator)我将在其中分配一些原始内存使用malloc,然后使用placementnew在原始内存上分配类型,例如#defineMYNEW(Type,Allocator)Allocator->Alloc(sizeof(Type));`templateType*Alloc(unsignedintsize)//Allocator::Alloc{return(Type*)new(malloc(reportedSize))Type;}但是,当Type没有默认构造函数时,我遇到了问题。我尝试过的一种情况

c++ - 使用抽象来制作翻译器

给定classAllocator{public:virtualchar*allocate(unsignedintsize)=0;//EFF:allocatesacharacterbufferofsizecharactersvirtualvoiddispose(char*buf)=0;//REQ:bufwasallocatedbythisallocator//EFF:releasememorypreviouslyallocated.};classTranslator{public:virtualchar*operator()(constchar*s,Allocator&a)=0;//EF