- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个分配大量内存并尝试查找匹配文档的查询处理器。每当我找到匹配项时,我都会创建一个结构来保存描述文档的两个变量,并将其添加到优先级队列中。由于无法知道我将执行多少次,因此我尝试使用 new 动态创建我的结构。当我从优先级队列中弹出一个结构时,队列(STL 优先级队列实现)应该调用对象的析构函数。我的结构代码没有析构函数,所以我假设在这种情况下会调用默认的析构函数。
但是,我第一次尝试创建 DOC 结构时,出现以下错误:
QueryProcessor.exe 中 0x7c812afb 处的未处理异常:Microsoft C++ 异常:内存位置 0x0012f5dc.. 处的 std::bad_alloc
我不明白发生了什么——我是不是用了太多内存以至于堆满了?这似乎不太可能。而且我以前甚至都没有用过那个指针。
所以:首先,我在做什么导致了错误,其次,下面的代码会多次工作吗?我是否需要为每个创建的结构创建一个单独的指针,或者我是否可以重复使用相同的临时指针并假设队列将保留一个指向每个结构的指针?
这是我的代码:
struct DOC{
int docid;
double rank;
public:
DOC()
{
docid = 0;
rank = 0.0;
}
DOC(int num, double ranking)
{
docid = num;
rank = ranking;
}
bool operator>( const DOC & d ) const {
return rank > d.rank;
}
bool operator<( const DOC & d ) const {
return rank < d.rank;
}
};
//a lot of processing goes on here
priority_queue<DOC, std::vector<DOC>, std::greater<DOC>> q;
//when a matching document is found, I do this:
rank = calculateRanking(table, num);
//if the heap is not full, create a DOC struct with the docid and rank and add it to the heap
if(q.size() < 20)
{
doc = new DOC(num, rank);
q.push(*doc);
doc = NULL;
}
//if the heap is full, but the new rank is greater than the
//smallest element in the min heap, remove the current smallest element
//and add the new one to the heap
else if(rank > q.top().rank)
{
q.pop();
cout << "pushing doc on to queue" << endl;
doc = new DOC(num, rank);
q.push(*doc);
}
非常感谢,bsg。
最佳答案
为什么要在堆上创建以下结构:
doc = new DOC(num, rank);
q.push(*doc);
这首先在堆上创建一个DOC
,然后将对象的拷贝存储在队列中,随后泄漏动态创建的DOC
。
以下内容就足够了,不会泄漏:
q.push(DOC(num, rank));
关于c++ - 将 new 用于结构 C++ 时出现 Bad_alloc 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575275/
这个错误的原因是什么?其实我不想受到之前测试用例结果的影响所以在测试用例开始时,我清空了队列以便每个测试用例都可以重新开始。 " #include #include using name
我的程序中出现了 bad_alloc 异常。 这些是限制条件: 1 #include #include #include class SuffixArray { std::vector
我在 digital ocean 上使用 node.js 并尝试运行文件上传/下载服务器。 为了确保服务器在后台运行并且不会因错误而退出,我使用了以下内容 nohup nodejs server.js
[前注:我已经阅读了 StackOverflow 中的现有线程。我的问题似乎没有] 我正在研究 Quake 2 MD2 格式。在新建一个指针数组后,我似乎得到了错误的分配。但是,如果我做一些可怕的指针
使用QAudioOut我试图按顺序播放存储在QByteArray中的数据...这在追加少量数据时起作用,但是当数据过多时,可以说是2到3个小时的RAW PCM从不同组合追加由于堆的大小不足以同时保存所
我是C++的新手,我真的很想使用Botan连接到硬件加密 token 。我不知道是否错过了libs或dlls的任何设置。 我基于Building Botan library in Windows 10
我正在用 C++ 编写一些使用 BFS 搜索迷宫的代码(我的主要语言是 Python,但我想稍微锻炼一下我的 C++ 大脑...),我偶然发现了这个奇怪的错误。 这里是相关的数据结构: struct
我必须为我的单元测试导致 bad_alloc(基本上,为了 100% 的代码覆盖率,我无法更改某些函数)。我该怎么办? 这是我的代码示例。我必须在这里的某个地方引起 bad_alloc。 bool i
在 c++ 中,分配内存时会发生 bad_alloc,或者至少我是这么理解的,但现在我在从数组读取内存时遇到此错误。我创建数组: int * possible = new int[ suma ]; 然
我遇到编译器错误: 错误 C2061:语法错误:标识符“bad_alloc” 我以前在其他项目中使用过下面的代码块,在 try block 中使用不同的内存分配,没有问题。我希望有人能向我解释为什么
我使用了这个伪代码: h := 1 /* Initialization of the pivot row */ k := 1 /* Initialization of the pivot colu
我四处寻找这个问题的解决方案,但找不到,所以我只好问问。我的程序是一个迷宫游戏,其中包含许多不同的房间,每个房间都使用指针链接到一个或多个相邻的房间。玩家从一个房间导航到另一个房间,直到找到导出。 当
我正在尝试递归地 segmentation 球体以在面上实现球面小波算法。虽然我有 8 GB 的可用内存,但我只能将二十面体 segmentation 为 9 个级别。我的问题是在构建我的球体时内存不
我从事一个游戏项目。最近,我们遇到了一个问题,当我们加载/卸载不同的场景大约 3 次时,我们捕获了“bad_alloc”异常。 每次加载场景时,我们首先将压缩的 .zip 文件夹加载到内存中,然后从中
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
当构造函数中抛出bad_alloc异常时,其中创建了多个对象,必须做些什么来清理内存。例如。 class Object { private: A * a; B * b;
我的程序抛出一个 std::bad_alloc。调试后发现是代码中抛出的 curFinalBucket->points.push_back(p); 其中点是一个 vector .在代码行之前,curF
我目前正在做一个 C++ 项目,现在我已经卡住了一段时间了。这是关于使用表达式模板和(至少对我来说)一个奇怪的 bad_alloc 的延迟评估。 如果您尝试下面的代码,您会注意到由于最后一次添加 b+
所以我有一个结构 struct float3 { float x, y, z; }; 我正在尝试创建一个函数来获取 x、y、z 值并将它们映射到键 0、1、2 以对应其各自的维度。我写了下面的
考虑这个简单的程序: #include #include int main(void) { const std::size_t size = 1 size_t_max / sizeof(i
我是一名优秀的程序员,十分优秀!