Search code examples
initializationsingly-linked-list

Conditional jump or move depends on uninitialised value(s) in LinkedList


I keep getting the Conditional jump or move depends on uninitialised value(s) in Valgrind for the function printList() and freeList(). I read a few posts and the problem was because head was not initialized after malloc() so I added newList->head = NULL after malloc in my createList() function. I am not sure how to fix this error. Any help is really appreciated! I am really new to C so I really don't know what the problem is...

typedef struct node{
    int year;
    int win;
    struct node* next;
}node_t;

typedef struct slist{
        node_t* head;
        int size;
}slist_t;

node_t* makeNode(int year, int win){
        node_t* newNode = (node_t*) malloc(sizeof(node_t));
        if (newNode == NULL){
            return NULL;
        }
        newNode->year = year;
        newNode->win = win; 
        return newNode;
}

void freeNode(node_t* node){
        if (node == NULL){
            return;
        }
        free(node);
}

slist_t* createList(){
    
        slist_t* newList = (slist_t*) malloc(sizeof(slist_t));
        
    if (newList == NULL){
            return;
    }   
    newList->head = (node_t*) malloc(sizeof(node_t));
    newList->head = NULL;   
    newList->size = 0;
    return newList;
}

node_t*  addFirst(slist_t* list, int year, int win){
    node_t* node = makeNode(year, win); 
    
    if (list == NULL){
        return;
    }
    
    if (node == NULL){
        return;
    }
    if (list->head == NULL){
            list->head = node;
    }   
    else{
        node->next = list->head;
        list->head = node;
    }
    list->size += 1;
    return node;
}

void printList(slist_t* list){
    if (list == NULL){
        return;
    }
    node_t* itr = list->head;
    while(itr != NULL){
        printf("%d, %d wins\n", itr->year, itr->win);
        itr = itr->next;
    } 
    printf("\n");
}
void freeList(slist_t* list){
    node_t* node = list->head;
    while (node){
        node_t* temp = node;
        node = node->next;
        free(temp);
    }
    free(list); 
}
int main(){
    slist_t* list = createList();
    
    addFirst(list, 2014, 71);
    addFirst(list, 2015, 78);
    addFirst(list, 2016, 93);
    addFirst(list, 2017, 93);
    addFirst(list, 2018, 108); 

    printList(list);
    freeList(list);
    
    return 0;
}

Solution

  • The problem is that makeNode does not initialise newNode->next. If it happens to have the value 0 (NULL), things will go fine, but try to add this statement in makeNode and then run the program:

    newNode->next = 1000000;
    

    Now it is more likely you get a segmentation fault or some other weird behaviour. So add this line instead:

    newNode->next = NULL;
    

    Not your issue, but this sequence of assignments produces a memory leak:

    newList->head = (node_t*) malloc(sizeof(node_t));
    newList->head = NULL;   
    

    After having allocated memory, you immediately lose the reference to it, by overwriting that reference with NULL. You should just drop the first of these two assignments. The first node is created later, when you call addFirst, so no node should be allocated here.