循环链表的内存错误:未分配正在释放的指针
Memory Error with Circularly Linked List: pointer being freed was not allocated
程序运行并且测试成功,但是在列表和测试结果之间我得到了这个:assignment_2.2(10729,0x7fff78db0300) malloc: * error for对象 0x7fb132d00000:未分配正在释放的指针
* 在malloc_error_break 中设置断点进行调试
知道我该如何解决这个问题吗?
main.cpp
#include "OLinkedList.h"
using namespace std;
int main(int argc, char** argv) {
OLinkedList CircleList;
CircleList.fillList(0);
CircleList.prntList();
CircleList.OTest();
return 0;
}
OLinkedList.h
#ifndef OLINKEDLIST_H
#define OLINKEDLIST_H
#include <iostream>
#include <iomanip>
using namespace std;
class OLinkedList{
private: struct Link{
int data;
Link *next;
};
Link *head;
Link *tail;
public: OLinkedList(){head = nullptr; tail = nullptr;};
~OLinkedList();
void fillList(int);
void prntList();
void OTest();
};
//Destructor for Class used to delete memory of list
OLinkedList::~OLinkedList(){
Link *linkPtr = head;
Link *nextPtr;
//traverses to the end of the list to delete memory
do
{
nextPtr = linkPtr->next;
delete linkPtr;
linkPtr = nextPtr;
}while(linkPtr!= nullptr);
}
//
void OLinkedList::fillList(int size){
Link *front = new Link; //create first link
head = front; //set first link = to traversal
head->data = size++; //Fill the front with data
head->next = nullptr; //Point the front to no where
Link *temp = head;
do{
Link *end = new Link; //Create a new link
end->data = size++; //Fill with data
end->next = nullptr; //Point to no where
temp->next=end;//Previous link will point to the end
temp=end; //Now this has become previous link
tail = end;
tail->next = head;
}while(size < 10); //Repeat until filled
}
void OLinkedList::prntList(){
Link *linkPtr;
linkPtr = head;
int i = 0;
do{
cout<<" "<<setprecision(3)<<linkPtr->data;
linkPtr = linkPtr->next;
i++;
}while(i < 10);
}
//Used to prove list is circular
void OLinkedList::OTest(){
Link *linkPtr = tail->next;
cout<<"\nAfter "<<tail->data<<" is "<<linkPtr->data;
}
#endif /* OLINKEDLIST_H */
当您将 tail->next 设置为 head 时,您正在按预期创建循环链表。您的 do while 循环应该检查 linkPtr != head,而不是 nullptr,因为您正在执行 double free on head。这是因为删除指针不会使之前指向它的任何变量无效,因此您的循环最终将 return 开始。
事实上,如果没有 double free,您的代码将进入无限循环。
程序运行并且测试成功,但是在列表和测试结果之间我得到了这个:assignment_2.2(10729,0x7fff78db0300) malloc: * error for对象 0x7fb132d00000:未分配正在释放的指针 * 在malloc_error_break 中设置断点进行调试 知道我该如何解决这个问题吗?
main.cpp
#include "OLinkedList.h"
using namespace std;
int main(int argc, char** argv) {
OLinkedList CircleList;
CircleList.fillList(0);
CircleList.prntList();
CircleList.OTest();
return 0;
}
OLinkedList.h
#ifndef OLINKEDLIST_H
#define OLINKEDLIST_H
#include <iostream>
#include <iomanip>
using namespace std;
class OLinkedList{
private: struct Link{
int data;
Link *next;
};
Link *head;
Link *tail;
public: OLinkedList(){head = nullptr; tail = nullptr;};
~OLinkedList();
void fillList(int);
void prntList();
void OTest();
};
//Destructor for Class used to delete memory of list
OLinkedList::~OLinkedList(){
Link *linkPtr = head;
Link *nextPtr;
//traverses to the end of the list to delete memory
do
{
nextPtr = linkPtr->next;
delete linkPtr;
linkPtr = nextPtr;
}while(linkPtr!= nullptr);
}
//
void OLinkedList::fillList(int size){
Link *front = new Link; //create first link
head = front; //set first link = to traversal
head->data = size++; //Fill the front with data
head->next = nullptr; //Point the front to no where
Link *temp = head;
do{
Link *end = new Link; //Create a new link
end->data = size++; //Fill with data
end->next = nullptr; //Point to no where
temp->next=end;//Previous link will point to the end
temp=end; //Now this has become previous link
tail = end;
tail->next = head;
}while(size < 10); //Repeat until filled
}
void OLinkedList::prntList(){
Link *linkPtr;
linkPtr = head;
int i = 0;
do{
cout<<" "<<setprecision(3)<<linkPtr->data;
linkPtr = linkPtr->next;
i++;
}while(i < 10);
}
//Used to prove list is circular
void OLinkedList::OTest(){
Link *linkPtr = tail->next;
cout<<"\nAfter "<<tail->data<<" is "<<linkPtr->data;
}
#endif /* OLINKEDLIST_H */
当您将 tail->next 设置为 head 时,您正在按预期创建循环链表。您的 do while 循环应该检查 linkPtr != head,而不是 nullptr,因为您正在执行 double free on head。这是因为删除指针不会使之前指向它的任何变量无效,因此您的循环最终将 return 开始。
事实上,如果没有 double free,您的代码将进入无限循环。