对结构 C++ 使用 new 时出现 Bad_alloc 异常 [英] Bad_alloc exception when using new for a struct c++
问题描述
我正在编写一个查询处理器,它分配大量内存并尝试查找匹配的文档.每当我找到匹配项时,我都会创建一个结构来保存描述文档的两个变量并将其添加到优先级队列中.由于无法知道我会这样做多少次,我尝试使用 new 动态创建我的结构.当我从优先级队列中弹出一个结构体时,队列(STL 优先级队列实现)应该调用对象的析构函数.我的结构代码没有析构函数,所以我假设在这种情况下调用了一个默认的析构函数.
I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamically using new. When I pop a struct off the priority queue, the queue (STL priority queue implementation) is supposed to call the object's destructor. My struct code has no destructor, so I assume a default destructor is called in that case.
但是,我第一次尝试创建 DOC 结构时,出现以下错误:
However, the very first time that I try to create a DOC struct, I get the following error:
QueryProcessor.exe 中 0x7c812afb 处未处理的异常:Microsoft C++ 异常:std::bad_alloc 在内存位置 0x0012f5dc..
Unhandled exception at 0x7c812afb in QueryProcessor.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f5dc..
我不明白发生了什么 - 我是否使用了太多内存以致堆已满?似乎不太可能.而且好像我以前也没有用过那个指针.
I don't understand what's happening - have I used up so much memory that the heap is full? It doesn't seem likely. And it's not as if I've even used that pointer before.
所以:首先,我在做什么导致错误,其次,下面的代码会运行多次吗?我是否需要为每个创建的结构体设置一个单独的指针,或者我可以重用相同的临时指针并假设队列将保留一个指向每个结构体的指针?
So: first of all, what am I doing that's causing the error, and secondly, will the following code work more than once? Do I need to have a separate pointer for each struct created, or can I re-use the same temporary pointer and assume that the queue will keep a pointer to each struct?
这是我的代码:
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.
推荐答案
为什么要在堆上创建以下结构:
Why are you creating the structure in the following on the heap:
doc = new DOC(num, rank);
q.push(*doc);
这首先在堆上创建一个DOC
,然后在队列中存储一个对象的副本,随后泄漏动态创建的DOC
.
This first creates a DOC
on the heap, then stores a copy of the object in the queue and subsequently leaks the dynamically created DOC
.
以下内容就足够了并且不会泄漏:
The following would be sufficient and doesn't leak:
q.push(DOC(num, rank));
这篇关于对结构 C++ 使用 new 时出现 Bad_alloc 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!