I'm having issues with Unload at the moment, I am getting Seg error. I am moving tho the code with GDB and error occurs on a Null check i am doing in a while Loop.

I am basically moving my transit pointer one node further every time I do not get null back. Then I count to make sure all 26 child array positions contain null and then free transit node? Hope that makes sense. But I eventually get the seg error on:-

if(temp -> child[j] == NULL)

any help would be great , thx


You say you are moving the transit pointer within a while loop but you have an if statement to check if the child is null. What condition do you have in your while loop, it should be something like:

while (temp->child[j]!=NULL)
  • I have two while loops the inner loop is checking for j != 27 , where J is the index of the of child array, so i am using :
    – Gordon
    Aug 20 '15 at 11:44
  • So it looks like you're using a trie, is that correct? As long as you followed the lecture on traversing the tree and freeing each successive child iteratively, you don't need to track j, you will automatically free all children if you have while (temp->child!=NULL). For that matter, your condition of j!=27 will not work. As an extreme example, consider the letter "q", you will only have a handful of children for that, so unless you defined each of the 26 children as NULL by default (an inefficient process), then you will get a seg fault for a sequence like q-b for which no words exist.
    – ronga
    Aug 22 '15 at 1:38
  • hi ronga thx. yes im using a trie. Of course your correct, not every node will have the 26 array position filled. So I will rewrite the method now and see how I get on. thanks again for your help : )
    – Gordon
    Aug 22 '15 at 15:37
  • hi Ronga, is it possible for me so send you my code to look at , Im going round in circles and cant figure out where im going wrong. Some where I am getting a seg error again?
    – Gordon
    Aug 23 '15 at 12:30
  • Consider what happens with the "q" example I gave. Starting from node q, if j=1, then the sequence is q-b, and there is no word in the english language that has a "qb". That means temp->child[1] is not defined (if you had initialized it to NULL, then it would be NULL and you'd have no problem, but it would be inefficient to malloc a node which you will never use).
    – ronga
    Aug 23 '15 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .