Conditional jump or move depends on uninitialised value - freeing a linked list

Posted by user720491 on Stack Overflow See other posts from Stack Overflow or by user720491
Published on 2012-11-24T10:41:02Z Indexed on 2012/11/24 11:06 UTC
Read the original article Hit count: 172

Filed under:
|
|
|

I want to free a linked list in C. All is working fine, but Valgrind is telling me

Conditional jump or move depends on uninitialised value(s)
    at 0x401400: mtf_destroy

Here's the code:

list_elt *head;

void mtf_init() {
    list_elt *current;
    head = malloc(sizeof(list_elt));
    current = head;
    for (int i = 0; i < LIST_SIZE-1; i++) {
        current->value = (BYTE) i;
        current->next = malloc(sizeof(list_elt));
        current = current->next;
    }
    current->value = LIST_SIZE-1;
}

void mtf_destroy(list_elt *elt) {
    if (elt->next != NULL)
        mtf_destroy(elt->next);
    free(elt);
}

How can I solve this? Thanks!

© Stack Overflow or respective owner

Related posts about c

    Related posts about linked-list