jjzjj

allocator

全部标签

C++ std::bad_alloc 错误

我正在开发C++程序(C++98)。它读取一个包含很多行(10000行)的文本文件。这些是制表符分隔值,然后我将其解析为Vector对象的Vector。然而,它似乎适用于某些文件(较小),但我的一个文件给我以下错误(该文件有10000行,大小为90MB)。我猜这是一个内存相关的问题?你能帮帮我吗?错误terminatecalledafterthrowinganinstanceof'std::bad_alloc'what():std::bad_allocAbort代码voidAppManager::go(stringcustomerFile){vector>vals=fileReader

c++ - 使用 boost::pool 管理 std::vector 中的内存分配

我想要一个std::vector对象,使用boost::pool分配对象。这样的事情是否正确:classMyClass{private:doubledata;public:MyClass(doubled):data(d){}};intmain(){std::vector>vect;vect.push_back(4.5);vect.push_back(9.8);//Arethesebeingstoredinapoolnow?return0;}此代码有效,但我不完全确定原因。我对分配器的概念很陌生,但如果我理解正确的话,这就是告诉std::vector使用池而不是默认分配器,因此在vect

c++ - STL 分配器和运算符 new[]

是否有使用operatornew[]的STL实现?作为分配器?在我的编译器上,生成Foo::operatornew[]private并没有阻止我创建vector...这种行为有任何保证吗? 最佳答案 C++标准,第20.4.1.1节。默认分配器allocate()函数使用全局运算符new:pointerallocate(size_typen,allocator::const_pointerhint=0);3Notes:Uses::operatornew(size_t)(18.4.1).

c++ - 为什么我得到 "cannot allocate an array of constant size 0"?

这个问题在这里已经有了答案:WhathappensifIdefinea0-sizearrayinC/C++?(8个答案)关闭8年前。我正在为学校做一个扫雷程序,但我的代码中一直出现这个错误cannotallocateanarrayofconstantsize0我不知道为什么会这样;我没有分配大小——我将它设置为0。另一个问题是,我如何通过char读取我的输入char,这样我就可以将它保存在我的数组?正如您在下面看到的,我正在使用输入和输出。我评论了我的输入和输出,这样你们就可以看到我在这个程序中使用了什么。我想通过char读取char,这样我就可以将所有map保存在数组中。我正在使用M

c++ - 为什么 std::allocator<>::deallocate() 有一个未使用的 size_type 参数?

使用std::allocator时,deallocate函数需要pointer参数,和一个size_type参数(std::allocator::deallocate(std::allocator::pointerp,std::allocator::size_type)。但是,没有使用size_type,也不是可选的。那么为什么它在那里?这让我很困惑,因为它应该是可选的,甚至不在那里,因为它没有在函数中使用.编辑:MSVC的分配器实现deallocatevoiddeallocate(pointer_Ptr,size_type){//deallocateobjectat_Ptr,igno

c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?

我有这个简单的代码:#include#includevoidfoo(conststd::vector>&networks){for(autop:networks){}}voidbla(conststd::vector>&networks){for(autop:networks){}}这会在bla()中产生一个错误:mrvn@frosties:~%g++-O2-W-Wall-g-std=gnu++17-cbla.ccInfileincludedfrom/usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,from/usr

c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'

代码A:vector::const_reverse_iteratorrcit;vector::const_reverse_iteratortit=v.rend();for(rcit=v.rbegin();rcit!=tit;++rcit)cout代码B:vector::const_reverse_iteratorrcit;for(rcit=v.rbegin();rcit!=v.rend();++rcit)coutCODEA工作正常但是为什么代码B通过错误:DEVC++\vector_test.cpp在'rcit!=std::vector::rend()与_Tp=int,_Alloc=s

c++ - 为什么 eglMakeCurrent 因 EGL_BAD_ALLOC 而失败?

我正在使用OpenGLES2.0和AndroidNDKr8b。我有一个用于工作线程的共享上下文。当我尝试使用eglMakeCurrent将共享上下文绑定(bind)到工作线程时,我收到错误EGL_BAD_ALLOC。现在让我感到困惑的是这段代码之前工作得很好......我不确定我做了什么来破坏它......EGL文档说这个错误与资源不可用有关,但我正在运行曾经在这个完全相同的设备上完美运行的同一个应用程序,并且所有纹理都可以从主线程正常加载。那么可能导致此错误的原因是什么?这是我的egl初始化:boolInitialize(void*displaySurface){assert(dis

c++ - 为什么 C++ 自定义分配器需要比较运算符?

在实现自定义C++分配器时,需要定义:operator==用于不同value_type的分配器operator!=用于不同value_type的分配器您可以在documentationofAllocatorconcept中查看自定义分配器的示例实现:#include#includetemplatestructMallocator{typedefTvalue_type;Mallocator()=default;templateconstexprMallocator(constMallocator&)noexcept{}T*allocate(std::size_tn){if(n>std::

c++ - 在 CUDA 中混合自定义内存管理和 Thrust

在我的项目中,我实现了自定义内存分配器以避免不必要地调用cudaMalloc一旦应用程序“预热”。此外,我使用自定义内核进行基本数组填充、数组之间的算术运算等,并希望通过使用Thrust来简化我的代码。并摆脱这些内核。设备上的每个数组都是通过原始指针创建和访问的(目前),我想使用device_vector和Thrust这些对象上的s方法,但我发现自己在原始指针和device_ptr之间转换一直以来,我的代码都有些困惑。我相当模糊的问题:您将/如何组织自定义内存管理的使用,Thrusts数组方法和以最易读的方式调用自定义内核? 最佳答案