哈希表 - 析构函数问题(未分配要释放的指针)

Hash table - issue with destructor (pointer being freed was not allocated)

提问人:510NH 提问时间:3/13/2020 最后编辑:510NH 更新时间:3/13/2020 访问量:214

问:

我有一个 HashTable,其中冲突是通过链接(链表)处理的。每个链表的第一个节点都有一个来自每个数组位置的指针。下面显示的是一个常规构造函数以及 3 个函数的规则。

尽管我的代码正在编译并且我的函数(添加、删除等)正在产生正确的输出,但我的析构函数有问题(IDE 使用线程 1:信号 SIGABRT 指向它),并且控制台显示“未分配指针未分配”在我的驱动程序完成运行后。我无法弄清楚出了什么问题,所以任何帮助将不胜感激。除了构造函数/析构函数之外,我没有包含任何其他函数(添加、删除等)的代码。

即使我注释掉了 copy 和 overloaded= 构造函数,析构函数仍然会出现同样的问题。

规范:

class HashTable {

public:

    HashTable(int);
    ~HashTable();
    HashTable(const HashTable &);
    HashTable& operator=(const HashTable &);

private:

    struct Node {
        string word;
        int wordCount;
        Node * next;

        // node constructor
        Node(string w, int count) {
            word = w;
            wordCount = count;
            next = nullptr;
        }
    };

    Node** wordList;
    int capacity;

    int hashFunction(string);
};

四大实施:

构造 函数:

HashTable::HashTable(int cap) {
    capacity = cap;
    wordList = new Node*[capacity];
    for (int i = 0; i < capacity; i++)
        wordList[i] = nullptr;   
}

析构函数(问题似乎出在哪里)

HashTable::~HashTable() {
    for (int i = 0; i < capacity; i++) {
        Node* curr = wordList[i];
        while (curr != nullptr) {
            Node* prev = curr;
            curr = curr->next;
            delete prev;          
        }
    }
    delete[] wordList;
}

复制构造函数:

HashTable::HashTable(const HashTable &obj) {
    capacity = obj.capacity;
    wordList = new Node*[capacity];
    for (int i = 0; i < capacity; i++) {
        if (obj.wordList[i] == nullptr)
            continue;
        Node * newNode = new Node(obj.wordList[i]->word,        
                                  obj.wordList[i]->wordCount);
        wordList[i] = newNode;
    }
}

复制赋值运算符:

HashTable& HashTable::operator=(const HashTable &obj) {
    if (this != &obj) {
        for (int i = 0; i < capacity; i++) {
            Node* curr = wordList[i];
            while (curr != nullptr) {
                Node* prev = curr;
                curr = curr->next;
                delete prev;
            }
        }
        delete[] this->wordList;

        this->capacity = obj.capacity;
        this->wordList = new Node*[capacity];
        for (int i = 0; i < this->capacity; i++) {
            if (obj.wordList[i] == nullptr)
                continue;                
            Node * newNode = new Node(obj.wordList[i]->word,
                                      obj.wordList[i]->wordCount);
            this->wordList[i] = newNode;
        }
    }
    return *this;
}
C++ 构造函数 Linked-List HashTable Rule-of-Three

评论


答:

2赞 1201ProgramAlarm 3/13/2020 #1

在复制构造函数和复制赋值运算符中,将列表指针从 复制到 .这会在两个对象中留下相同的指针,一旦一个 HashTable 被释放,就会导致双重释放和其他问题,objthis

当你做复制时,你需要做一个深度复制,即为单词列表的复制分配新的节点。

评论

0赞 510NH 3/13/2020
谢谢!我就是这样做的,但现在我的代码无法编译,因为在我的复制构造函数下指示的行中出现“线程 1:EXC_BAD_ACCESS (code=1, address=0x0)”错误。
0赞 510NH 3/13/2020
我通过在复制和重载赋值构造函数中的每个 for 循环下添加一个 if 语句来修复该错误。但现在我又回到了原来的错误。