我有一个项目从一个文件计数。由于这是一个学校项目,我不能使用大多数的图书馆,但基本的图书馆。因此我决定使用哈希映射。但我的链表给出了一个空指针异常。准确地说是“异常抛出:读取访问冲突 这个是0xCCCCCCCC发生”。我搜索了很多找到解决方案,但我找不到任何东西。感谢您的时间。链接列表空指针错误C++
public: liste() {
head = NULL;
tail = NULL;
}
void createnode(string value) {
liste();
node* temp = new node;
temp->data = value;
temp->next = NULL;
if (head == NULL) { // get error here!!!!
head = temp;
tail = temp;
temp = NULL;
}
else {
tail->next = temp;
temp = tail;
}
}
int main()
{
struct site {
int count;
string name;
};
unsigned long int i=0;
unsigned int x=0;
ifstream theFile("access_log");
string word;
liste* table[100000];
site list[10];
while (theFile >> word) {
if (word.find(".")!=-1) {
if (word.find("H") == -1) {
x=(int)hashG(word);
if (x < 100000) {
table[x]->createnode(word);
}
}
}
}
for (int i = 0; i < 100000; i++) {
cout << table[i]->length() << " " << table[i]->getData() << endl;
}
return 0;
}
这是一个常规错误,指示您的程序触发了一些未定义的行为。这可能意味着你在某处有指针错误 - 写入内存不应该。 (另外,你的程序不完整。) – metal
'liste * table [100000];'然后'table [x] - > createnode(word);'。你在哪里分配/初始化'table [x]'? –
哦,我没有意识到,我需要分配/初始化它。我看过的所有教程都没有。 – Redout