简体   繁体   中英

Using uninitialized memory pointer in C

I am creating a general library for working with lists in C. I want to implement this method called destroy that will deallocate memory for this list by freeing each of its elements, but the compiler keeps giving me this warning, although everything works fine in a program that I made using this library:

Warning C6001 Using uninitialized memory '*pointer'.

How can I prevent this warning?

void destroy(list* first_elem)
{
    item* pointer = *first_elem;

    if (*first_elem == NULL) {
        return;
    }
    while (pointer->next != NULL) { //here's the warning
        item* toKill = pointer;
        pointer = pointer->next;
        free(toKill);
    }
    free(pointer);
    *first_elem = NULL;
}

Definitions of item and list:

typedef struct item_struct {
    Set value;
    struct item_struct* next;
} item;
typedef item* list;

I assume when you are creating item struct, you didn't initialize "item_struct *next" pointer.

item i;
i.value = ....;
i.next = NULL; // Add this into your code.

Using uninitialized variables is an undefined behaviour. Hence it can cause problems. Even if your code is running okey in your computer, this doesn't necessarily mean it will work another computer without an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM