在C++中创建队列 - 什么是 malloc 错误?
Creating a Queue in C++ - What is a malloc error?
我正在创建一个简单的队列,但是我遇到了一个奇怪的错误:
编译(46096,0x7fffb263d380( malloc:* 对象 0x7f9c61c00340 的错误:未分配正在释放的指针 *在malloc_error_break中设置断点进行调试
如果我减少排队方法的数量,我只会得到一个分段错误。
我的主要课程:
#include <iostream>
#include "queue.h"
using namespace std;
template <typename ItemType>
void enqueue(const ItemType &i, node<ItemType>* &n, const long &p) {
node<ItemType> *nn = new node<ItemType>;
nn->item = i;
nn->next = n;
n = nn;
}
template <typename ItemType>
ItemType dequeue(node<ItemType>* &n) {
ItemType fr;
node<ItemType> *ptr;
ptr=n;
n = n->next;
while(ptr->next != NULL){
ptr = ptr->next;
}
fr = ptr->item;
delete ptr;
return fr;
}
int main() {
node<int> *head;
enqueue(13,head, 1);
enqueue(10,head, 2);
enqueue(18,head, 3);
cout<<head->item<<"t"<<head->next->item<<"t"<<head->next->next->item<<endl;
cout<<dequeue(head)<<"t";
cout<<dequeue(head)<<"t";
cout<<dequeue(head)<<endl;
//cout<<dequeue(head)<<endl; // create error cathcing
}
我的节点包装器类:
#include <iostream>
template <typename ItemType>
struct node {
ItemType item;
node<ItemType> *next;
int priority;
};
你这样做:node<int> *head;
,然后用nn->next = n;
排队
这最终会存储未初始化的内存,然后在删除时失败。
试试node<int> *head = NULL;
相关文章:
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 在C++中创建队列 - 什么是 malloc 错误?
- 错误:malloc:对象 0x7f9edf504080 的 *** 错误:未分配正在释放的指针
- 如何修复对象在释放后可能修改的malloc错误
- 使用 std::shared_ptr 时出现 malloc 错误
- 库"curl_easy_cleanup()"导致 malloc 错误 (C++)
- 无法解决 opencv 代码中的 malloc 错误
- `delete []`on`int*````new'''给出了malloc错误
- 在启用 OpenCL 选项的情况下使用 Tesseract 时出现 malloc 错误
- malloc错误“命名管道打开时内存损坏(快速)
- 使用libmx链接并使用std::stringstream时发生g++malloc错误
- 使用一个类的两个单独实例时出现Malloc错误2372
- Mac OSX上的Qt App中的完全随机malloc错误
- c++错误:如何解决对象的malloc: ***错误.未分配被释放的指针
- 矢量重新分配导致malloc错误
- OSX如何在用c++扩展python时调试malloc错误
- c++中向量的Malloc错误
- 使用BigInteger数组时出现Malloc错误
- 将字符串流定义为标头中的成员时发生Malloc错误
- 我需要帮助来定位我的 malloc 错误