jjzjj

allocator

全部标签

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

c++ - std::containers 的日志分配器?

X:我需要知道程序的每个部分使用了多少内存。我的程序经常使用C++std库。特别是,我想知道每个对象使用了多少内存。我是怎么做的:要记录some_vector的消耗,只需写my::vectorsome_vector;在哪里namespacemy{templateusingvector=std::vector>;}登录分配器实现如下:templatestructLoggingAllocator{//...boilerplate...pointerallocate(size_typen,std::allocator::const_pointerhint=0){log_allocation(

c++ - 程序以 std::bad_alloc 终止

我正在运行一个C++程序,它在任意点死于std::bad_alloc,这取决于指定的输入。以下是关于该计划的一些观察/要点:对于较短的运行(运行时间取决于输入),程序会正常完成。问题只出现在较大的运行中。该程序没有任何可检测到的内存泄漏。这已通过Valgrind/Memcheck进行了较小的运行检查。此外,我的整个代码没有任何指针(所有动态分配都是由库完成的,例如,在std::vector和std::string中;它是这些库类中的分配失败),因此内存泄漏的可能性极小。几个对象在循环中分配,然后移动到容器中。这些对象中有几个是为了在程序快结束之前一直存在。我怀疑堆碎片可能是个问题(请参

c++ - 将临时变量写入 Json : I get\u0000

我面临一个奇怪的问题:当我尝试在for循环中添加一个Json变量时,它没有正确写入输出文件,而它在循环外运行良好(rapidJsonv0.11)。编辑:循环不是问题,但即使只有括号也会出现错误这是我的代码示例:rapidjson::Documentoutput;output.SetObject();rapidjson::Document::AllocatorType&allocator=output.GetAllocator();{std::strings1("test");output.AddMember("test_field",s1.c_str(),allocator);}std

c++ - 为什么在抛出 'std::bad_alloc' 实例后调用终止?

每1秒,函数工作一次。我的系统是linux。奔跑突然死亡。-----global-------staticintarrayNum[33000];-------------------function(){unsignedshortint**US_INT;US_INT=newunsignedshortint*[255];for(inti=0;i程序停止。和留言↓在抛出“std::bad_alloc”的实例后终止调用what():std::bad_alloc 最佳答案 bad_alloc异常是由内存分配失败触发的(因此您的new之一)。

c++ - 自定义分配器如何知道指针是否指向数组?

我可能误解了有关分配器的一些概念。但是我真的不知道为什么我们需要传递一个std::size_t的参数给allocate来指示要分配的对象的数量。参数是否用于分配内存区域,如数组或vector?如果它们是,那么我的自定义分配器如何知道先前返回的指针是指向一个区域还是指向一个对象?我的分配器是否负责记录这些指针?如果他们不是,那我们为什么需要那个参数? 最佳答案 allocator_traits>::allocate预期为T类型的连续N个对象序列分配内存.N由size_t提供参数。Iftheyare,thenhowcanmycustom

c++ - std::allocator 是否处理 C++17 中的过度对齐类型?

C++17引入了std::aligned_alloc和对齐感知new可以进行过度对齐分配,但是std::allocator?它是否处理过度对齐的类型? 最佳答案 在N4659(C++17DIS)中,23.10.9.1[allocator.members],bullet2T*allocate(size_tn);Returns:Apointertotheinitialelementofanarrayofstorageofsizen*sizeof(T),alignedappropriatelyforobjectsoftypeT.与C++1