0

I have ran speller in valgrind to see if there was memory leaks and its appears that all memory allocated have been freed. But, I get many errors of type : "Conditional jump or move depends on uninitialised value(s)". In searching in the questions already asked on this board, I found that it's probably ans issue related to the initialization of my new nodes. I think the problem is when my load function check if the next node to create, isn't it yet, valgrind fail because there is no memory allocated to the "un-existing node" to create... I'm very confuse with this notion. Help would really be appreciate to solve this. There is how I initialize my root and new nodes in load function :

Global declaration in dictionary.c :

node* root = NULL;
node* current = NULL;

and in LOAD :

....
**87** root = malloc(sizeof(node));    --->> LINE 87 <<---
word_counter = 0;
int index = 0;
current = root;

// looks for word until end reached
for (int c = fgetc(inptr); c != EOF; c = fgetc(inptr))
{
    // looking words one by one
    if (c != '\n')
    {
        if (c == '\'')
        {
            index = 26;

            if (current->children[index] == NULL)
            {
                current->children[index] = calloc(1, sizeof(node));
            }
        }

        else
        {
            index = c - 'a';

            **112** if (current->children[index] == NULL) -->> LINE 112 <<--
            {
                current->children[index] = calloc(1, sizeof(node));
            }
        }

        // update current pointer to the next node
        current = current->children[index];
    }

    else
    {
        current->is_word = true;
        word_counter++;
        current = root;
    }
}

fclose(inptr);
return true;
}

finally, here transmission of valgrind :

enter image description here

2
  • can you post the lines between 87 and 122 from your code (in the load function), which are the ones referred to by valgrind? And please put a comment to the side indicating which lines are those. Also, what does your code do when current->index[children] is not NULL?
    – Irene
    Nov 16, 2015 at 20:06
  • Thanks @Irene for your time. I have added where are those lines. Actually if current->children[index] is not NULL I do nothing, I pass on and continue forward to the next character... If the node is already created, it's not necessary to take no further more actions... at least I thought. My LOAD function loads correctly all the dictionary words, but maybe memory allocations are altered.
    – CharlesD89
    Nov 17, 2015 at 0:20

2 Answers 2

1

Try using calloc instead of malloc (the syntax is slightly different, but you can google it, it's not difficult). The difference is that calloc initializes all the memory (in this case you might want to zero initialize it).

malloc doesn't. So, on line 112, when you do

if (current->children[index] == NULL) 

Here there's no guarantee that it will (or won't) be NULL, because you never initialized the block of memory.

It's as if you did:

int a;

if (a != 0)

Either use calloc or manually initialize all the memory to NULL (you'll need a loop).

0

I had the same issue with valgrind errors of type : "Conditional jump or move depends on uninitialised value(s)"

Initially, after the malloc, I had not initialized each of the Node*, and this was the ultimate cause of the valgind error report.

To resolve the issue for each malloc, I setup a loop to initialize each of the *node[27] with NULL. That cleared the issue for me. Good Luck

You must log in to answer this question.

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