0

I have run ./test which processes this function:

bool unload(void)
{
    //free up memory from linked list
    for (int i = 0; i < 26; i++)
    {
        if (hashtable[i] != NULL)
        {
            node *cursor = malloc(sizeof(node));
            cursor = hashtable[i];
            while(cursor != NULL)
            {
                node *temp = malloc(sizeof(node));
                temp = cursor;
                cursor = cursor -> next;
                free(temp);
            }
            free(cursor);
        }
    }

return true;

}

However, help50 valgrind ./test tells me that I am leaking memory, and I should specifically look at when I malloc space for cursor to see if I free it. I don't know where the leak happens.

Thanks in advance~

1
  • As a side note, you could create temp before the for loop so it is only created and destroyed once instead of once per node. – Cliff B Jul 11 '18 at 1:28
0

Easy enough. This is a somewhat common issue for new programmers. Look at the code:

            node *temp = malloc(sizeof(node));
            temp = cursor;

This code is declaring a pointer called temp, initializing it with a malloc (meaning that memory is being allocated), and then reassigning a different address from another pointer, thus losing the address of the memory that was just allocated. This is the leak.

Note that declaration and initialization of a variable are two separate and distinct processes. A var can be declared without being initialized, although that's a bad practice. There are two "good practice" methods to use here. First, declare the var and initialize it with a null:

     node *temp = NULL;

This declares and initializes it without allocating any memory. It also tells the system that it doesn't contain a real address. It points at nothing. If you just did node *temp; it would be uninitialized and would contain whatever garbage data that was in memory at that time. The consequences of that could be disastrous. (A test for NULL would fail, it could contain an invalid address that would throw a runtime error, or worse, it could contain random data that matches a valid address allocated to the current program with unpredictable results!)

However, since you're immediately reassigning the contents, why not do it more efficiently?

            node *temp = cursor;

Simple enough?

The important lesson here is that when you declare a pointer, you're not REQUIRED to malloc memory to it, but you should always initialize it somehow so that it doesn't contain garbage data. ;-)

Of course, I haven't examined whether the function is working correctly, but that's a different question! ;-D

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • thank you cliff! – faelesterio Jul 11 '18 at 1:47
  • You're welcome. Can you accept the answer so that it doesn't sit in the unanswered question pool forever? Thanks. – Cliff B Jul 11 '18 at 1:59

You must log in to answer this question.

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