0

I'm pretty sure I'm just being dumb, but I've been wracking my brain and I'm drawing a blank. I've completed 5/6 of the functions in Speller, but the unload function has me stumped- the description goes "Unloads dictionary from memory, returning true if successful, else false". Obviously it's possible for me to use valgrind, but are you supposed to code valgrind into your program or something, otherwise how would the program be able to tell? I feel like I'm missing something really obvious, but I've been tying myself in knots trying to figure it out, and I thought I'd ask the community.

0

If it's possible for you to run valgrind, it's possible for the automated checker to do so as well. In the tideman program there were checks for internal structures that couldn't be observed externally.

1
  • Thanks for your answer, do you know how I could go about coding it into my program? – BlueKhakis Feb 24 at 3:27
0

Valgrind is an analysis program. It can be used to determine if there is a memory leak, but not to fix the problem. Typically if there is a memory leak, it's due to a use of malloc, without a paired free.
To implement a variable size dictionary, there will be a linked list of nodes. Each time a node was added, did you need a malloc? If so, when it's time to unload the dictionary, traverse the linked list, calling free for each node.
Earlier programs had a simple use case: call malloc once, use it, free it.
This more complicated program will call malloc a variable number of times in a row, followed by the use cases, eventually followed by the same number of calls to free.
One thing to watch for is calling free on the first node in the chain, but not having a pointer to the next node. The number of calls to free should be the same number as the calls to malloc.

1
  • Hi juls, thank you for your answer, but my question was on a slightly different track, I can't figure out how it's possible to tell whether free() has been successful/unsuccessful and how to implement this in my code. That is presumably what the stipulation "Unloads dictionary from memory, returning true if successful, else false" is referring to? – BlueKhakis Feb 25 at 7:45
0

Write the unload function and return true. You, the programmer, need to assume that any node free'd by free(node) has been free'd. If the program encounters a "problem" with a free it may seg fault, or crash in some other way. Finally, run the program under valgrind to make sure all the nodes are freed.

Basically the return true; is telling speller, yes I have finished running the unload function. It is not meant to tell speller that the unload function has free'd all the nodes. This is true for all the functions, the return value indicates "doneness", not "correctness".

2
  • Thanks, this makes more sense. So is the else false part of Unloads dictionary from memory, returning true if successful, else false just a mistake? – BlueKhakis Feb 25 at 19:26
  • 1
    Maybe a mistake, maybe just clumsily worded. In any case, a cautionary tale about comments. Reading and writing them. – DinoCoderSaurus Feb 26 at 2:52

You must log in to answer this question.

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