C++删除错误
C++ delete error
我是C++的新手,正在尝试编写一个基于链表的队列。我的测试程序运行良好,直到我在出列函数中添加了delete。然后我得到错误:对象0x7f8a61403a00的malloc:***错误:释放的指针未分配
如有任何帮助,我们将不胜感激。
#include <string>
#include "queue.h"
#include <iostream>
using namespace std;
Queue::Queue() { // Constructs a new empty queue.
current_size = 0; // number of elements in queue
front_p = NULL; // first element in queue
back_p = NULL; // last element in queue
}
Queue::Queue( const Queue& q ) {// Copy constructor.
// nothing to copy if queue empty
if (q.current_size == 0) {
return;
}
// queue not empty
else {
node * p = q.front_p;
node * n;
// assign front pointer
n = new node(p -> data, NULL);
front_p = n;
p = p -> next;
// middle elements
while (p -> next != NULL) {
n -> next = p;
n = new node(p -> data, NULL);
p = p -> next;
}
// assign back pointer
n = new node(p -> data, NULL);
back_p = n;
current_size = q.current_size;
}
}
void Queue::enqueue( int item ) { // Enqueues <item> to back
node * n = new node(item, NULL);
// first item in queue, front & back ptrs point to same element
if (back_p == NULL) {
front_p = n;
back_p = n;
}
else {
back_p -> next = n;
back_p = n;
}
current_size++;
}
int Queue::dequeue() { // removes and returns the front item.
// empty queue
if (current_size == 0) {
return -1;
}
int item = front();
node * p = front_p;
if (current_size == 1) {
front_p = NULL;
back_p = NULL;
delete p;
current_size--;
return item;
}
else {
front_p = front_p -> next;
delete p;
current_size--;
return item;
}
}
测试文件
//test file
#include <iostream>
#include "queue.h"
using namespace std;
int main(void) {
Queue q1;
cout << "Create a new queue q1" << endl;
cout << "Size of q1 t" << q1.size() << endl;
cout << "Is q1 empty? t" << q1.empty() << endl;
cout << endl << endl;
cout << "enqueue t1,2,3,4,5" << endl;
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
cout << "front of q1 t" << q1.front() << endl;
cout << "size of q1 t" << q1.size() << endl << endl;
cout << "q2 is a deep copy of q1" << endl;
Queue q2(q1);
cout << "front of q2 t" << q2.front() << endl;
cout << "size of q2 t" << q2.size() << endl << endl;
cout << "removed 4 from q1" << endl;
q1.remove(4);
cout << "removed 2 from q2" << endl;
q2.remove(2);
cout << endl;
cout << "print out remaining elements of q1" << endl;
int N = q1.size();
for(int i=0; i < N; i++) {
cout << q1.dequeue() << " ";
}
cout << endl << endl;
cout << "print out remaining elements of q2" << endl;
N = q2.size();
for(int i=0; i < N; i++) {
cout << q2.dequeue() << " ";
}
cout << endl;
}
头文件
class Queue
{
public:
Queue(); // Constructs a new empty queue.
Queue( const Queue& q );// Copy constructor.
~Queue();// Destructor.
void enqueue( int item ); // Enqueues <item>.
int dequeue(); // Dequeues the front item.
int front(); // Returns the front item without dequeuing it.
bool empty(); // Returns true iff the queue contains no items.
int size(); // Returns the current number of items in the queue.
bool remove(int item); // If <item> occurs in the queue, removes the
// first occurrence of <item> and returns true; otherwise returns false.
private:
class node // node type for the linked list
{
public:
node(int new_data, node * next_node){
data = new_data ;
next = next_node ;
}
int data ;
node * next ;
};
node * front_p ; // pointer to the (node containing the) next item
// which to be dequeud, or NULL if the queue is empty.
node * back_p ; // pointer to the (node containing the) last item
// which was enqueued, or NULL if the queue is empty.
int current_size ; // current number of elements in the queue.
};
代码还有其他问题,但最直接的问题是您的复制构造函数已损坏。
在这个周期
while (p -> next != NULL) {
n -> next = p;
n = new node(p -> data, NULL);
p = p -> next;
}
p
是指向源队列q
的元素的指针。同时,n
是新队列的一个元素。通过执行n -> next = p;
,您正在使新的队列节点链接到源队列节点链中。这创建了一个完全无意义的节点链接结构,该结构稍后会分解。
相关文章:
- 删除指向指针的指针是运行时错误吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 从矢量中删除元素后出现隔离错误
- 删除映射和分割错误中的一个过去结束元素
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 删除堆后对瓦尔格林德错误感到困惑
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 删除链表中的节点 - 分段错误
- 尝试从动态分配的二维数组 C++ 中读取值时出现分段错误,并在尝试删除它时给出 munmap_chunk():
- 为什么删除分配的阵列会导致内存错误?
- 错误:使用已删除的函数"std::unique_ptr<...>
- 如何修复从矢量中删除元素的错误?
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 尝试删除指向派生对象的基指针时断言错误
- 错误:当我尝试使用引用时,使用已删除的功能
- 编译时错误:删除了联合默认构造函数
- 提升d_ary_heap/priority_queue编译错误:删除函数