【发布时间】:2013-11-19 06:47:34
【问题描述】:
我有一个节点哈希表。在我的构造函数中,我将哈希表的每个元素都初始化为 NULL。我的代码可以编译,但可执行文件结束后,出现以下错误: “调试断言失败!” 程序:/路径名/ 文件:f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp 行:52
表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
我的构造函数和析构函数的实现:
// Constructor
Hash::Hash() {
for(int i = 0; i < tableSize; i++) {
Hash_Table[i] = NULL; // Will be used to indicate an empty bucket
}
}
//
//
// Destructor
Hash::~Hash() {
song* temp;
song* temp_next;
for(int i = 0; i < tableSize; i++) { // For every bucket
if(NULL != Hash_Table[i]) { // If the bucket is not already empty
temp = Hash_Table[i];
while(NULL != temp) {
delete[] temp->artist;
delete[] temp->title;
delete[] temp->playlists;
delete[] temp->album;
temp_next = temp->next;
delete temp;
temp = temp_next;
} // end while loop
Hash_Table[i] = NULL;
} // end if
} // end for loop
delete[] Hash_Table;
} // end destructor
【问题讨论】:
-
您要删除的某些指针一定是无效的。您可以添加一些
cerr跟踪来记录指针和对它们的操作,看看有什么不匹配的,但我建议您使用std::unordered_map<>或std::map<>和std::string- 你会发现生活更轻松。 -
显示更多代码。
Hash_Table的类型是什么?在代码中没有new。delete[]这里也显得不合理。 -
你需要一个调试器来定位问题。