0

From my understanding a segfault is usually caused by invalid memory access or leak? But I can't figure out where it is. Is there a way like a program that will go through your code and figure out where it is causing this error? Please help.

Lilith X
  • 83
  • 1
  • 8

1 Answers1

0

When you use a debugger, you should see that you get an access violation at if (cur->symbol == word[i]). (Do try this at home.) Why does this happen?

In DictionaryTrie::insert, if curr (== root) is a nullptr, you allocate a node and store it in root, but you do not update curr. You then enter the for loop, and reference curr->symbol. Since curr is a null pointer, you get an access violation.

The simple fix is to assign

curr = root;

after assigning a value to root.

1201ProgramAlarm
  • 31,926
  • 7
  • 42
  • 52