Skip to main content
added 18 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

It's a common problem. The problem lies in load(), where you have created a "shadow variable."

A shadow variable happens this way: First, the original variable is created, either earlier in main or a function, or as a global variable. Then a second variable is created with the same name, but will have local scope because it's within a set of curly braces and the original var is outside those braces. The original variable, with more global scope is masked by the local shadow variable that takes precedence.

In this case, you've created a global variable called root. Then, you create a shadow var of root inside load().

node* root=NULL; 

All of the work is done with the shadow root to build the trie, but when load ends, the shadow var ceases to exist and the entire trie is lost. All this time, the global root remains and is still set to NULL (or garbage data). When check() is called, the code hits the line if(trav->children[index] == NULL). Since trav==root==NULL, the struct, including the children[ ] pointers, doesn't exist, so it generates a seg fault.

If you remove node* from the line in load(), the problem goes away.

There are more issues, but those would be new questions. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;)

It's a common problem. The problem lies in load(), where you have created a "shadow variable."

A shadow variable happens this way: First, the original variable is created, either earlier in main or a function, or as a global variable. Then a second variable is created with the same name, but will have local scope because it's within a set of curly braces and the original var is outside those braces. The original variable, with more global scope is masked by the local shadow variable that takes precedence.

In this case, you've created a global variable called root. Then, you create a shadow var of root inside load().

node* root=NULL; 

All of the work is done with the shadow root to build the trie, but when load ends, the shadow var ceases to exist and the entire trie is lost. All this time, the global root remains and is still set to NULL. When check() is called, the code hits the line if(trav->children[index] == NULL). Since trav==root==NULL, the struct, including the children[ ] pointers, doesn't exist, so it generates a seg fault.

If you remove node* from the line in load(), the problem goes away.

There are more issues, but those would be new questions. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;)

It's a common problem. The problem lies in load(), where you have created a "shadow variable."

A shadow variable happens this way: First, the original variable is created, either earlier in main or a function, or as a global variable. Then a second variable is created with the same name, but will have local scope because it's within a set of curly braces and the original var is outside those braces. The original variable, with more global scope is masked by the local shadow variable that takes precedence.

In this case, you've created a global variable called root. Then, you create a shadow var of root inside load().

node* root=NULL; 

All of the work is done with the shadow root to build the trie, but when load ends, the shadow var ceases to exist and the entire trie is lost. All this time, the global root remains and is still set to NULL (or garbage data). When check() is called, the code hits the line if(trav->children[index] == NULL). Since trav==root==NULL, the struct, including the children[ ] pointers, doesn't exist, so it generates a seg fault.

If you remove node* from the line in load(), the problem goes away.

There are more issues, but those would be new questions. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;)

Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

It's a common problem. The problem lies in load(), where you have created a "shadow variable."

A shadow variable happens this way: First, the original variable is created, either earlier in main or a function, or as a global variable. Then a second variable is created with the same name, but will have local scope because it's within a set of curly braces and the original var is outside those braces. The original variable, with more global scope is masked by the local shadow variable that takes precedence.

In this case, you've created a global variable called root. Then, you create a shadow var of root inside load().

node* root=NULL; 

All of the work is done with the shadow root to build the trie, but when load ends, the shadow var ceases to exist and the entire trie is lost. All this time, the global root remains and is still set to NULL. When check() is called, the code hits the line if(trav->children[index] == NULL). Since trav==root==NULL, the struct, including the children[ ] pointers, doesn't exist, so it generates a seg fault.

If you remove node* from the line in load(), the problem goes away.

There are more issues, but those would be new questions. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;)