jjzjj

allocator

全部标签

c++ - bad_alloc 在初始化和填充 vector 时?

在尝试生成随机数vector时,我偶然发现了一个std::bad_alloc错误。这是我的代码:#include"search.h"#include"gtest/gtest.h"int_size=100;std::vectorGetSortedVector(intsize){//initvectorstd::vectorv(size);//fillwithrandomnumbersfor(std::vector::size_typei=0;iv=GetSortedVector(_size);//nothingmovesfartherthanthisline}附注:我现在确实在使用gen

c++ - 尽管未定义 <new> header ,但 New 会抛出 bad_alloc?

new是怎么回事?程序中的表达式可以抛出bad_alloc尽管没有#include还是出错(因为这个错误isdefinedintheheader)?来自3.7.4。N3337的:Thelibraryprovidesdefaultdefinitionsfortheglobalallocationanddeallocationfunctions.Someglobalallocationanddeallocationfunctionsarereplaceable(18.6.1).AC++programshallprovideatmostonedefinitionofareplaceablea

c++ - 为什么STL要为Allocator预留接口(interface)?

STL为什么要为Allocator预留接口(interface)?以vector为例:template>classvector;因为我们有很多选择来分配内存和构造对象,比如operatornew,delete,new[],delete[],它几乎可以做我们创建对象时需要做的任何事情。那么,为什么像vector这样的STL容器需要一个Allocator接口(interface),如果我们不分配一个,它在大多数情况下都是默认的std::allocator?为什么不直接使用新的表达式呢?如果目的是使用户定义的分配行为成为可能,为什么不让用户提供他们自己定义的operatornew、new[]

c++ - 使用 'g++' 结果为 "warning: will never be executed"

我继承了一个C++项目。我在RHELbuild5.5与GCC4.1.2通过makefile。该项目很大(数百个文件),总的来说代码还不错。然而,在编译过程中,我经常收到一个GCC警告,上面写着(prefix"/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2"):/bits/allocator.h:Inconstructor‘std::allocator::allocator()[with_Tp=char]’:/bits/allocator.h:97:warning:willneverbeexecuted

c++ - 为什么 `e.what()` 打印 "bad allocation"?

tryblock中的new表达式在我的计算机中引发了bad_alloc异常。请注意,catch子句按值而不是按引用接收异常对象。为什么e.what()会打印出"badallocation"?我以为它会被切成薄片。#includeintmain(){try{int*p=newint[0x1F000000];}catch(std::exceptione){std::cout 最佳答案 VisualStudio(Dinkumware?)使用std::exception的实现,其中包含消息的内部存储†。(完成一个接受字符串的非标准构造函数。

c++ - 在抛出 'std::bad_alloc' what(): std::bad_alloc 的实例后终止调用

我的程序中出现了bad_alloc异常。这些是限制条件:1每个字符串的长度最多为100000,并且只包含小写字符。由于这些限制,我无法弄清楚为什么我的程序得到bad_alloc。#include#include#include#includeclassSuffixArray{std::vectorsuffixes;size_tN;public:SuffixArray(std::string&s){N=s.length();suffixes.resize(N);for(size_ti=0;i>T;std::vectorresults;for(inti=0;i>str;SuffixArra

c++ - 如何在STL中使用unordered_set?

我需要一个C++(STL)中的hash_map类。主要操作是将对放入集合中,然后检查它是否存在。我找不到示例代码来判断我声明的内容是否正确。#include#includeusingnamespacestd;usingnamespace__gnu_cxx;typedefpairpis;structeqpis{booloperator()(pisp1,pisp2)const{if(p1==p2)returntrue;returnfalse;}};intmain(){hash_map,eqpis>map;}这个编译。但是,如果我添加以下行:map[pis(10,"你好")]=10;然后它给

c++ - 最小化浪费的字节以对齐 2 个 header 之间的数据(自定义分配器)

这是自定义分配器中的内存布局:-^towardlessaddress....Header[size=16alignment=4]....(1)somewastespaceA[size=A(unknown)]content[size="SIZE"alignment="ALIGN"]....(2)somewastespaceB[size=B(unknown)]Header[size=16alignment=4]....(3)....vtowardmoreaddressHeader的确切地址事先未知。但是,我知道:-everyHeaderaddress%4==0from(1,3)"conte

c++ - boost pool_alloc

为什么boost::fast_pool_allocator建立在单例池之上,而不是每个分配器实例一个单独的池?或者换句话说,为什么只提供那个,而不是每个分配器都有一个池的选项?那样做会不会是个坏主意?我有一个类在内部使用大约10种不同的boost::unordered_map类型。如果我使用了std::allocator,那么在它调用delete时所有内存都会返回给系统,而现在我必须在某些时候对许多不同的分配器类型调用release_memory。我自己推出使用池而不是singleton_pool的分配器是否愚蠢?谢谢 最佳答案 分

c++ - forward_list、set、list 等如何调用 std::allocator?

我注意到分配器只能分配T类型的对象并保留大小为n*sizeof(T)的内存块.std::list内部的链表节点然而,类型不一定是T类型的对象,它们的大小也不一定与T相同对象。那样的话,怎么可能std::list使用std::allocator分配内存? 最佳答案 这就是为什么rebindtype存在。它允许您创建一个类似的分配器,而不是分配其他东西(例如node)。基本上是这样的:std::allocatorint_alloc;std::allocator::rebind>node_alloc;//Perhapsmoreuseful