You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

哈希表析构函数触发段错误,如何正确实现析构逻辑?

Fixing the Segmentation Fault in Your HashTable Destructor

Let's break down what's wrong with your current destructor and fix it step by step—plus address other hidden issues that could cause problems down the line:

Key Bugs Causing the Segfault

Your original destructor has three critical mistakes that trigger invalid memory access:

  • You're overwriting the array pointer: The line head = head->getNext(); corrupts your original pointer to the hash table array. After the first loop iteration, you're no longer pointing to the array, leading to garbage memory access.
  • You're trying to delete array elements directly: destList = &head[i]; grabs the address of an element in your dynamically allocated array. These elements are part of the array itself—you shouldn't delete them individually; only the entire array needs deletion at the end.
  • You nullify the array pointer before deleting it: Setting head = NULL before delete [] head; turns the deletion into a no-op, leaving the array memory leaked and breaking cleanup logic.

Corrected Destructor Implementation

Here's the fixed version with clear comments explaining each step:

HashTable::~HashTable(){
    // Iterate through every bucket in the hash table array
    for(int i = 0; i < TABLESIZE; i++){
        Hash* currentNode = head[i].getNext(); // Start with the first linked list node (not the bucket)
        
        // Traverse and delete all nodes in the bucket's linked list
        while(currentNode != NULL){
            Hash* nextNode = currentNode->getNext(); // Save next node before deleting current
            delete currentNode;
            currentNode = nextNode;
        }
        
        // Reset the bucket's next pointer for cleanliness
        head[i].setNext(NULL);
    }
    
    // Delete the entire hash table array
    delete [] head;
    head = NULL; // Avoid dangling pointer
}

Additional Fixes to Prevent Memory Issues

Your code has a few other hidden problems that could cause leaks or unexpected behavior:

  1. Hash Class Key & Destructor Fix:
    Your Hash::setKey just assigns raw char* pointers, which will leak memory if you ever use dynamically allocated strings. Update the Hash class to properly manage string memory:

    Hash::~Hash(){
        delete [] key; // Free dynamically allocated key memory
    }
    
    void Hash::setKey(char* tempKey){
        delete [] key; // Free existing key first
        // Allocate new memory and copy the input string
        key = new char[strlen(tempKey) + 1];
        strcpy(key, tempKey);
    }
    
  2. Remove Invalid delete newNode in addNode:
    The line delete newNode; at the end of addNode destroys the node you just added to the linked list, creating dangling pointers. Delete this line entirely.

  3. Fix String Comparison Logic:
    Comparing char* with == checks pointer addresses, not actual string content. Use strcmp instead for valid string comparisons:

    // Replace this:
    while(head[index].getKey() != tempValue && head[index].getKey() != "EMPTY")
    // With this:
    while(strcmp(head[index].getKey(), tempValue) != 0 && strcmp(head[index].getKey(), "EMPTY") != 0)
    

After applying all these changes, your destructor will no longer cause segmentation faults, and your hash table will manage memory safely.

内容的提问来源于stack exchange,提问作者Jonathan Vazquez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:16:08