我試圖實現散列表,但我得到循環的運行時錯誤createHashTable()函數。任何人都可以告訴我爲什麼會顯示這個「運行時錯誤」?它是否是StackOverflow錯誤?任何人都可以告訴我爲什麼顯示「運行時錯誤」?
#include <iostream>
using namespace std;
#define LOAD_FACTOR 20
struct ListNode{
int data;
struct ListNode *next;
};
struct HashTableNode{
int bCount; // number of elements in the block
struct ListNode *next;
};
struct HashTable{
int tSize; // table size
int count; // total number of elements in the table
struct HashTableNode **hashTableNodeArray;
};
int hashFunction(struct HashTable *h, int data){
return data % h->tSize;
}
struct HashTable * createHashTable(int numberOfElements){
struct HashTable *h = new HashTable;
h->count = 0;
h->tSize = numberOfElements/LOAD_FACTOR;
h->hashTableNodeArray = new HashTableNode *[h->tSize];
for(int i = 0; i < h->tSize; ++i){
// this is where it is showing runtime error
h->hashTableNodeArray[i]->bCount = 0;
h->hashTableNodeArray[i]->next = nullptr;
}
return h;
}
void deleteHashTable(struct HashTable *h){
struct ListNode *node, *tmp;
for(int i = 0; i < h->tSize; ++i){
node = h->hashTableNodeArray[i]->next;
while(node != nullptr){
tmp = node;
node = node->next;
delete tmp;
}
}
delete[] h->hashTableNodeArray;
delete h;
}
int main(int argc, char **argv){
struct HashTable *h = createHashTable(220);
deleteHashTable(h);
return 0;
}
發佈有關運行時錯誤的更多信息。 – 2015-02-05 12:55:41
可能因爲您以某種您不應該使用指針的方式導致[* undefined behavior *](http://en.wikipedia.org/wiki/Undefined_behavior)。使用調試器查找*發生崩潰的位置*。 – 2015-02-05 12:56:22
鄧諾。你在調試時發現了什麼? – 2015-02-05 12:56:53