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.
Asked
Active
Viewed 104 times
0
-
3Use a debugger. – Vlad from Moscow Feb 03 '20 at 20:03
-
1[What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Feb 03 '20 at 20:05
-
Please provide a [mcve] with focus on minimal – Thomas Sablik Feb 03 '20 at 20:19
1 Answers
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