【发布时间】:2023-03-08 19:13:01
【问题描述】:
我目前正在处理一个家庭作业问题,我一直在试图弄清楚为什么我不断收到分段错误(核心转储)。我已将其范围缩小到访问函数内的数组。该程序的目标是将书中的单词存储在单独的链接哈希表中。而且我们不能使用 STL。
有问题的数组(表大小为30000):
Node* hashTable = new Node[TABLE_SIZE];
节点:
struct Node {
unsigned int hash = 0;
string word;
int count;
Node* next;
};
我正在使用的函数原型:
int insert(unsigned int hash, string word, Node* table[]);
我是如何调用函数的:
int result = insert(hashIndex, word, &hashTable);
函数本身(是的,我知道它不漂亮):
int insert(unsigned int hash, string word, Node* table[])
{
unsigned int hashIndex = hash;
Node* newNode;
newNode->hash = hashIndex;
newNode->count = 1;
newNode->word = word;
newNode->next = nullptr;
if(table[hashIndex % TABLE_SIZE] == nullptr) {
table[hashIndex % TABLE_SIZE] = newNode;
return 0;
}
else {
if(table[hashIndex % TABLE_SIZE]->hash == hash) {
table[hashIndex % TABLE_SIZE]->count++;
return 2;
}
Node* indexPtr = table[hashIndex % TABLE_SIZE]->next;
while(indexPtr) {
if(indexPtr->hash == hash) {
indexPtr->count++;
return 2;
}
indexPtr = indexPtr->next;
}
indexPtr->next = newNode;
}
return 1;
}
每当我尝试访问 hashTable 中的任何内容时,都会遇到分段错误。任何帮助和批评将不胜感激。
最小可重复示例:
#include <iostream>
#include <cctype>
using namespace std;
struct Node {
unsigned int hash = 0;
string word;
int count;
Node* next;
};
const string FILENAME = "C:/Users/Matthew/CLionProjects/p5/ulyss12.txt";
const int TABLE_SIZE = 30011;
string processWord(string word);
string removePunctuation(string word);
unsigned int hashFunc(string value);
int insert(unsigned int hash, string word, Node* table[]);
void displayHash(Node* table[]);
int main()
{
Node* hashTable = new Node[TABLE_SIZE];
string word = "HelloWorld";
unsigned int hash = hashFunc(word);
insert(hash, word, &hashTable);
return 0;
}
string processWord(string word)
{
if(word.length() >= 5) {
word = removePunctuation(word);
}
else {
return "";
}
return word;
}
string removePunctuation(string word)
{
int i = 0;
while(ispunct(word[i])) {
word.erase(i, 1);
i++;
}
i = word.length()-1;
while(ispunct(word[i])) {
word.erase(i, 1);
i--;
}
return word;
}
int insert(unsigned int hash, string word, Node* table[])
{
unsigned int hashIndex = hash;
Node* newNode = new Node();
newNode->hash = hashIndex;
newNode->count = 1;
newNode->word = word;
newNode->next = nullptr;
if(table[hashIndex % TABLE_SIZE] == nullptr) {
table[hashIndex % TABLE_SIZE] = newNode;
return 0;
}
else {
if(table[hashIndex % TABLE_SIZE]->hash == hash) {
table[hashIndex % TABLE_SIZE]->count++;
return 2;
}
Node* indexPtr = table[hashIndex % TABLE_SIZE]->next;
while(indexPtr) {
if(indexPtr->hash == hash) {
indexPtr->count++;
return 2;
}
indexPtr = indexPtr->next;
}
indexPtr->next = newNode;
}
return 1;
}
unsigned int hashFunc(string value)
{
const char* str = value.c_str();
int length = value.length();
unsigned int hash = 5381;
int i = 0;
for (i = 0; i < length; ++str, ++i)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
【问题讨论】:
-
"...我们不能使用 STL。"叹。另一门 C+ 课程。
-
??????小心前行,看看会发生什么?
-
在
Node* hashTable = new Node[TABLE_SIZE];中,hashtable指向Nodes 的数组。在int insert(unsigned int hash, string word, Node* table[])中,table是指向Nodes 的指针数组的指针。如果您将hashTable填充到table中,那么您将度过一段糟糕的时光,因为它们是不同的类型。也许你应该展示你是如何调用这个函数的。 -
哈希表的大小应使用素数。不要使用包含很多因素的数字。
-
我猜
hashTable中的指针未初始化。请出示minimal reproducible example