0

I implemented the tree data structure for pset6. It's all doing it right. On checking with valgrind it is even freeing the memory. The valgrind shows that all the blocks were freed. But the error summary is as follows

ERROR SUMMARY: 9912829 errors from 9 contexts (suppressed: 0 from 0)

Probably the cause of the errors is in my freenode function which I implemented the code for my freenode function called by bool unload() is as follows. Please solve my problem.

/**
* Frees  all the node
*
*
*/
void freenode(node* unloader)
{   
    for (int i = 0; i < 27; i++)
    {
        if (unloader -> children[i] != NULL)
        {
            freenode(unloader->children[i]); 
        }
    }

    if (unloader != NULL)
    { 
        free(unloader);   
    }   
}

The cause told by valgrind is

Conditional jump or move depends on uninitialised value(s)

Please solve my problem.

2

The cause you have pasted means that you are checking the value of a variable you didn't previously specify (most likely unloader -> children[i] != NULL). When creating a new node, you should initialize all children node pointers to NULL to make sure they don't contain any uninitialized, "garbage" memory.

5
  • It is initialized to root in the unload function. This isn't the problem. Jun 25 '14 at 11:04
  • @AbdulHannanAli But your children are not. You probably have the children as a member of your node, uninitialized. And I am talking about all the children of all the nodes, not specifically the root one.
    – skreborn
    Jun 25 '14 at 11:07
  • my errors size reduced from a thousands to hundreds by setting the initial value of all children to NULL in load but still I have to figure why are the 200 coming now. Jun 25 '14 at 11:46
  • @AbdulHannanAli Well, in that case, you could try Kareem's suggestion on using the --track-origins=yes flag to see where the problem originates from.
    – skreborn
    Jun 25 '14 at 12:07
  • Thanks skreborn you really helped me. After hours of struggling I was able to debug my program. Thanks alot. Jun 25 '14 at 14:20
3

When implementing a recursive function, the base case should be written first because it's the first thing that should be executed to determine whether there should be one more recursive call or not!

What you're doing, basically, is that you're executing the loop no matter whether the base case is reached and that's a bad practice and might sometimes cause some errors.

You may compile your program with the -ggdb3 flag and execute valgrind with the flag --leak-check=full. This will give you information about what and where exactly valgrind thinks the errors are in your code.

clang -ggdb3 prog.c -o prog
valgrind --leak-check=full ./prog

Update: for your error, you may check also use the --track-origins=yes to track the origin of uninitialized values as this answer suggests!

3
  • But Is there any base case for void function? I implemented it but it gives me an error. Jun 25 '14 at 11:02
  • 1
    Yes, surely there is! You may use return; to end the execution of a void function immediately!
    – kzidane
    Jun 25 '14 at 11:04
  • Thanks Kareem Mesbah your answer was really helpful in debugging process. Kareem you are a super man. Jun 25 '14 at 14:21

You must log in to answer this question.

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