Linked List clear()函数和析构函数

时间:2016-08-02 20:57:25

标签: c++ pointers linked-list implementation dynamic-memory-allocation

我正在处理链接列表实施。我需要使用clear()函数删除一些动态内存,但它给了我一个错误:

  对象0x100105400的

malloc:***错误:未分配的指针

当我注释掉clear()函数时,我的析构函数给出了同样的错误,但是delete的位置。

从错误消息中,我认为我删除了未分配的内存,但我无法找到它。你觉得我在这里做错了什么?你有什么建议吗?

template <class T>
void LinkedList<T>::clear(){
    node* temp = head;
    while (temp != NULL){
        head = head->next;
        delete temp;
        temp = head;
    }
    head = NULL;
    tail = NULL;
    size = 0;
}

template <typename T>
LinkedList<T>::~LinkedList(){
    node* current = head;
    while (head != NULL){
        current = head->next;
        delete head;
        head = current;
    }
    size = 0;
 }

以下是我的Flight.cpp文件:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
#include <vector>
#include "Flight.h"

Flight::Flight(){

}

Flight::~Flight(){
    LinkedList<LinkedList<ArrivalCity>>::node* temp =    flightWeb.getNodeAtIndex(0);
    while(temp != NULL){
        temp->data.clear();
        temp = temp->next;
    }
    flightWeb.clear();
}

void Flight::modifyCitiesLinkedList(ArrivalCity& city_1, ArrivalCity& city_2){

int position = 0;

//check if city1 existed. if yes, just add the object of arrival city to corresponding list. if no, create a new node to flightWeb and add object to corresponding list
if(startingCityRepeated(city_1.getName(), position) == true){
    flightWeb.getNodeAtIndex(position)->data.addNode(city_1);
} else {
    LinkedList<ArrivalCity>* newNode = new LinkedList<ArrivalCity>;
    newNode->addNode(city_1, city_1.getName());
    flightWeb.addNode(*newNode, city_1.getName());
}

position = 0;
//check if city2 existed. if yes, just add the object of arrival city to corresponding list. if no, create a new node to flightWeb and add object to corresponding list
if(startingCityRepeated(city_2.getName(), position) == true){
    flightWeb.getNodeAtIndex(position)->data.addNode(city_2);
} else {
    LinkedList<ArrivalCity>* newNode = new LinkedList<ArrivalCity>;
    newNode->addNode(city_2, city_2.getName());
    flightWeb.addNode(*newNode, city_2.getName());
}
}

//return false: no matching cities existed
//return true: found the matching city
bool Flight::startingCityRepeated(std::string city, int& position){

//no elements in the list, return false, no need to check
if(flightWeb.getSize() == 0){
    return false;
}

LinkedList<LinkedList<ArrivalCity>>::node* temp = flightWeb.getNodeAtIndex(0);

while(temp != NULL){
    //city node existed, return true;
    if(temp->name == city){
        return true;
    }
    temp = temp->next;
    position++;
}
//no matching name found, return false
return false;
}


int main(int argc, const char* argv[]) {

Flight flight;
flight.readFlightRoute(argc, argv);
//flight.printStartingCities();
//flight.printArrivalCities();
//flight.totalAvailableFlight();

return 0;
}

1 个答案:

答案 0 :(得分:2)

您注释掉clear()功能的事实并没有告诉我们什么;您真正注释掉的是 clear()的所有调用,或者您确实注释掉clear(),这意味着clear()未被调用从您的代码中,(否则您的程序将不再编译),并且注释掉未调用的函数无效。

以下内容:

while (current != NULL)
while (head != NULL){

是两个(严重缩进的)嵌套循环,这可能不是你想要做的。

没有一个完整的,自包含的,可运行的例子很难说,但可能会发生的事情是内循环的第一次执行将清除你的列表,然后外循环将再次迭代,两者都是currenthead指向已经解除分配的最后一个节点,从而指出错误。

而不是所有这些复杂的东西,只需从析构函数中调用clear()