0

First, valgrind flags the file pointer at the fopen call, and also the malloc call to the root pointer. It designates both still reachable in loss record.

Second, since for pset5 I check for an end of a trie branch by if characters[i] == NULL, how should I allocate memory to root and children nodes? Do I need to loop through each child initializing index to NULL like I have for root, or can I just use calloc(1,sizeof(node))

    // check for end of word or file
    while (c != '\n' && c != EOF);
    {

[removed most of the solution code - mod]

1
  • I realize now there is a ";" after the while loop...now after showing this to everyone.
    – Tom L.
    Commented Aug 4, 2015 at 21:31

2 Answers 2

0

The semi-colon is what caused the hub-bub.

0

Valgrind, your file might not be closed under certain circumstances (like quitting a function quick).

If you check NULL after jumping into the child, then it's too late for you to change parent node's child. Instead, check the child before jump. If it's NULL, then allocate memory to it. Jump in.

Both malloc() and calloc() works. For malloc(), each time you allocate some memory, it's best practice to initialize the value or you might get garbage values. Garbage for conditions is bad.

calloc(), on the other hand, initialize every bit to zero. Sometimes, you don't really have to initialize everything if you're 100% sure you will assign a value soon, without condition checks on it yet. So, calloc() can waste your few milliseconds worth of time.

Either works just fine. I'm using malloc(), though.

1
  • 0 down vote I'm reaching a different but related issue-- Valgrind is giving me a an error of "Conditional jump or move depends on uninitialised value(s)" when I use: if(trav -> children[i] == NULL) I'm trying to check if the child has been initialized yet, but it won't even allow me to test it, because the value is uninitialized. I then tried setting all of the children pointers to NULL, but that didn't work either. Commented Nov 11, 2017 at 21:08

You must log in to answer this question.

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