正在为堆上的priority_queue分配数据
Allocating data for the priority_queue on heap
我使用C++priority_queue数据结构来存储大量数据。然而,我总是会遇到分段错误,可能是由于堆栈溢出错误。我的优先级队列定义是:
typedef struct cell {
int x;
int y;
float value;
}*cell_p;
struct cell_comparator {
bool operator()(cell_p arg1, cell_p arg2) {
return arg1->value > arg2->value;
}
};
priority_queue<cell_p, vector<cell_p>, cell_comparator>* open;
和在主要功能:
open = new priority_queue<cell_p, vector<cell_p>, cell_comparator>();
int x, y;
for (x = 0; x < nXSize; x++) {
for (y = 0; x < nYSize; y++) {
int index = (y * nXSize) + x;
cell_p c = (cell_p) malloc(sizeof(cell));
c->x = x;
c->y = y;
c->value = input_buffer[index];
open->push(c);
}
}
作为一个容器类,我使用了vector,但它似乎将数据存储在堆栈上,而不是堆上。如何告诉priority_queue将数据存储在堆上?
然而,我总是遇到分段错误,可能是由于堆栈溢出错误。
你的分析很可能是错误的。几乎没有理由认为在您显示的代码中的任何地方都存在堆栈溢出。
如何告诉priority_queue将数据存储在堆上?
它已经做到了。
解决这个问题的一个好方法是在获得segfault时查看堆栈跟踪(并在问题中包括堆栈跟踪)。
如果这没有帮助,那就拿出一个SSCCE并发布它。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 在c++中使用动态分配的问题
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 为什么我可以使用比分配的内存更多的内存
- 使用RAII在给定次数的迭代后重新分配资源