0

I hope you are well. I am now on Pset5's unload function and have run into a blocker.

When I run valgrind on it, here's what happens:

http://pastebin.com/45XHAcPj

I cannot see what the issue is.

Kind regards,

Adi (a CS50 student from London, UK)

Here's the code that I am using:

node* temp = NULL;
node* cursor = NULL;
for(int i = 0; i < 26; i++)
{
    first = hashTable[i];
    cursor= first;

    while(cursor != NULL)
    {
        temp = cursor;
        cursor = cursor->next;
        free(temp);
        if (strchr(cursor->wordArray,'\'') == NULL || !isalpha(cursor->wordArray))
        {
            break;
        }
    }
    ++unloadCounter;
}

if(unloadCounter == 26)
{
     return true;
}
return false

2 Answers 2

0

Although this is a well constructed question I must say it is a little difficult to know exactly what you're looking for. Is it a semantic error?

However to try and give you some perspective; for one thing you aren't properly freeing 6 blocks of memory, total heap usage: 8 allocs, 2 frees, 1,472 bytes allocated, "still reachable" implies that the memory requested is not freed, although it is really not necessary to free those blocks since they are allocated near-termination of your program, so the OS will reclaim the memory very soon. See this answer on SO.

Your valgrind call spits out a lot of information not (necessarily) applicable to your case, line 45 and downwards to 76 spits out memory redirects between function calls, called a traceback or stack trace, this is a result of the --memcheck:track-origins=yes flag included. From line 12 up until line 76 "syms" or "symbol table" is mentioned frequently, the symbol table is a data structure that defines what variables were declared where and the scope they belong to.

This answer is admittedly very generic and won't directly solve your problem, but without a clue of what the problem is, I'm sorry to say I cannot say how to solve it. I hope at least it will guide you the right way, good luck!

Edit: I see you posted your actual code while I was typing this answer, I have to go now but I'll see if I can get look at it later if no one else replies.


Update:

Something is not being freed in load() at line 89 according to valgrind at line 112. That is the only thing not being freed according to valgrind at line 110 "loss record 1 of 1".

This is now getting me thinking whether or not I have free'd in the wrong order or not

As long as you didn't free anything you really shouldn't in load() you're probably clear on that end. In unload(), according to valgrind, you free everything you once allocated.

Edit: Actually, since the memory allocated in load() shall be freed in unload() you might indeed have a leak, I'll look over your code.

8
  • The problem that I am having right now is that valgrind is saying that I have not free'd everything. This is starting to make me think that I have not free'd the linked lists inside of my hashtables in the wrong order. Commented Oct 22, 2016 at 15:59
  • See my updated answer. Commented Oct 22, 2016 at 16:17
  • I saw your updated answer. If I free what was malloc'ed in line 89 within the load function then that would mean check and size would not have any nodes to work with. Commented Oct 22, 2016 at 16:30
  • I'm sorry for my brainfart there. If your text is 6 words and 6 nodes are not freed, then you've freed nothing. I'm not entirely sure but it seems your free statement is never reached. Right now I'm too preoccupied to look any deeper but know that the issue lays in unload(). Commented Oct 22, 2016 at 16:54
  • I know that you might be pre-occupied right now but just for clarity I should mention that I created my own dictionary and text files. Both of them are 6 words long. Commented Oct 22, 2016 at 18:10
0

Just thought that I would post a quick update for you. I can now confirm that after some time with the folks at my local Office Hours I have finished the unload function.

First of all, each of the words from the text had to be converted to lowercase words.

Second of all, I was not inserting new items into my hash table in the correct order!

After I fixed those things Valgrind showed no leaks and I was getting hyped!

After doing some little fixes (i.e. removing some debug code) I was able to speed up the program twofold!

Thanks for everyone's help. I have now moved onto Problem Set6(the web server PSet).

You must log in to answer this question.

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