将指针数组传递给函数时出现分段错误

Segmentation fault when passing array of pointers to function

我目前正在处理一个家庭作业问题,我一直在试图弄清楚为什么我总是遇到分段错误(核心转储)。我已将其缩小到访问函数内的数组。该程序的目标是将书中的单词存储在单独的链散列 table 中。而且我们不能使用STL。

有问题的数组(Table 大小为 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;
}

感谢@AlanBirtles、@user4581301 和@tadman 提供的答案。事实证明我没有将数组中的节点初始化为任何东西,所以一个简单的 for 循环遍历 hashTable 数组并将所有内容设置为 nullptr,以及上述用户的建议解决了我遇到的问题.

解决方案

  Node** hashTable = new Node*[TABLE_SIZE]{};

感谢 @Eljay 提出的使用大括号而不是 for 循环的建议。

如果您注意到任何其他问题。请评论。或者什么的。