Uninitialized value was created by a heap allocation
The heap allocation you assign to dictionary
uses malloc
which does not initialize the returned bytes. So dictionary
in the code you've posted ends up being an array of uninitialized pointers. Presumably you go on to use those pointers in some way which valgrind knows to be an error.
An easy way to fix this is to use calloc
instead of malloc
, because it zeros the returned bytes for you. Or, use memset
to zero the bytes yourself.
In the updated code you use an uninitialized pointer:
dictionary = malloc(sizeof(node*) * LISTS);
// .... code that does not change dictionary[i] for any i
new_node->next = dictionary[index]; // use uninitialized pointer
As people had wrote already, this will only work if you had pre-set all the pointers to be NULL
before entering this loop:
dictionary = malloc(sizeof(node*) * LISTS);
if ( !dictionary ) {
return false;
}
for (size_t i = 0; i < LISTS; ++i) {
dictionary[i] = NULL;
}