valgrind says i have conditional jump or move depends on initialized value(s)

Viewed 769

I am having an issue where valgrind is saying that i have a conditional jump or the move depends on initialized values

#include <stdio.h>
#include<stdlib.h>

ListNodePtr convertBSTtoLinkedList(TreeNodePtr root)
{
    ListNodePtr newNode;
    ListNodePtr leftLinkedList;
    ListNodePtr rightLinkedList;
    ListNodePtr head;

    if (root == NULL)
    {
        return NULL;
    }

    newNode = malloc(sizeof(struct ListNode));
    newNode->key = root->key;
    head = newNode;

    if(root->left != NULL)
    {
        leftLinkedList = convertBSTtoLinkedList(root->left); 
        head = leftLinkedList;

        ListNodePtr conductor;
        conductor = leftLinkedList;
        while (conductor->next != NULL)
        {
            conductor = conductor->next;
        }

        conductor->next = newNode;

    }

    if(root->right != NULL)
    {
        rightLinkedList= convertBSTtoLinkedList(root->right); 
        newNode->next = rightLinkedList;
    }

    return head;
    }

valgrind the problem is somewhere in the while loop and i am assuming it is because i havent initialized that conductor->next is NULL, but i dont know how to do that since it it checking if it is NULL

2 Answers

The problem is here:

newNode = malloc(sizeof(struct ListNode));
newNode->key = root->key;
head = newNode;

You create a new node and set the key, but you don't set the next pointer. While the value does get set if there is a right subtree, it will not get set if the node is a leaf node.

When your recursive call for the left side returns after processing a leaf node, it then checks the next pointer which was never written to. By reading an uninitialized value, you invoke undefined behavior.

Make sure that you initialize this pointer to NULL when you create the node:

newNode = malloc(sizeof(struct ListNode));
newNode->key = root->key;
newNode->next = NULL;
head = newNode;

Here what you can do is when you create a node in some place in your code initialize next with a NULL. That will most likely close this message of valgrind.

So add this line in your code

newNode->next = NULL;

The problem arises because the value that you are checking to be NULL is not initialized with NULL. As a result this checking is as if it is with some uninitialized value.

The solution I mentioned is just intializing every node with a proper next and key value.


Few points:

Check if the return value of malloc whether it failed or succeeded.

if ( newNode == NULL)
{
   fprintf(stderr,"Error");
   exit(1);
}
Related