Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

Unload function is pset5, seg.fault

+1 vote
asked Jan 25, 2015 in Pset5 by toncaa (200 points)

Need help here, pset5 unload function.I am using trie.
SO i am getting segmentation fault, hard to see why...So any idea ?
http://pastebin.com/r2bau8W7

1 Answer

0 votes
answered Jan 25, 2015 by Faïza Harbi (11,960 points)
edited Jan 29, 2015 by Faïza Harbi

Your logic is..you can do much easier.

You should check that base is not equal to NULL before using it in your while loop

As for your clearMem:

you can do it much simpler: you have a linked list, the last element points to NULL.

To free node after node wiithout losing the list:

create a temp node* that will point to the children[i]->next.,

then children[i] points to temp, here you have both temp and base pointing at the same list(on 2 different levels), except there is children[i] that can be free'd since his children is dealt by temp. And so on so forth untill NULL.

 

Also, you use an int that can only be equal to 0 or  right?  if the list is free'd, one for the other cases.

That's how boolean works. You'd better use a boolean, true if you're done, false otherwise, init to false.

here, is it a representation of your trie? IS it how your base works?

https://twitter.com/HarbiFaiza/status/560685581156765696

commented Jan 25, 2015 by Faïza Harbi (11,960 points)
Also, a void method returns nothing, your return is a syntax error.
As for your trie... You should have for the root, that can point to an array of char, and each char can point to another char in another array of char.
The second part is missing. If a word is of length 30, you never get deep enough in your trie to free that word.
commented Jan 25, 2015 by toncaa (200 points)
edited Jan 25, 2015 by toncaa
Thanks for feedback !
Ok now checking if it is NULL before i call clearMem with base.
Well i can't see the point there, how can i delete element before that deleted in linked list if that is not double oriented linked list? I need to call it from start ( base/root ) again.
Hmm isn't return and nothing same ?It just returns from the function.
I don't see why it can't get deep enough in my trie to free that word ? I think it will.Explain me plz.
So why is my implementation not working still not getting it ...
commented Jan 26, 2015 by Faïza Harbi (11,960 points)
i going from 0 to 26 is fine, the question is , how many times do you need to do that? look at the trie drawing again, you see that each array is a 27 long array (a to z plus apostrophe plus \0 to point to to tell C this word is over, like first array : C, c points to , A in the second array , A points to T in the  third arraywhich point to \0 in the fourth array . That is something you are missing. and the 0 and 1 doesn't cut it, recursion is a good idea, don't get me wrong, you start with all the arrays, and you go up and up and up freeing all slots from each array at each turn of a loop you have, it's the rest you 're missing.

As for void: void returns nothing you pass something in, it does what it does, and that's it. returning something of type void, knowing the meaning of the word void, nothingness, you can see it has no logic. Plus simply write return...It works depending on the function you do that in. But it's the thing to avoid.
commented Jan 26, 2015 by toncaa (200 points)
Ok thanks for feedback, now it is running but valgrind reports unallocated space.
this is new version http://pastebin.com/r2bau8W7
commented Feb 3, 2015 by Faïza Harbi (11,960 points)
unallocated space as in too many free? Because if so, it's normal, since you built your unload based on how you load: you change one, you change the other basically. Sorry for the late answer, I was looking for this question and it was under my nose for days and managed to miss it :)
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

187 questions

248 answers

217 comments

3,056 users

10 Online
0 Member And 10 Guest
...