哈希表析构函数触发段错误,如何正确实现析构逻辑?
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 = NULLbeforedelete [] 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:
Hash Class Key & Destructor Fix:
YourHash::setKeyjust assigns rawchar*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); }Remove Invalid
delete newNodeinaddNode:
The linedelete newNode;at the end ofaddNodedestroys the node you just added to the linked list, creating dangling pointers. Delete this line entirely.Fix String Comparison Logic:
Comparingchar*with==checks pointer addresses, not actual string content. Usestrcmpinstead 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

