简体   繁体   中英

a warning in c :C6001 Using uninitialized memory '*now'

I don't know why this warning occurs I tried to write a code to delete the repeated data in the linked list and the point that I realized is that when I comment the line which contains free command the warning won't happen! and I got this warning in the visual studio 2019 compiler please tell me what is it for and how can I fix this problem

#include <stdio.h>
#include <stdlib.h>
typedef struct list List;
struct list
{
    int data;
    List* next;
};
List* remove_repetition(List* list)
{
    List* pre = list;
    if (pre == NULL)
        return NULL;
    List* now = list->next;
    while (now != NULL)
    {
        if (pre->data == now->data)//the warning occurs here!
        {
            pre->next = now->next;
            List* del = now;
            now = now->next;
            free(del);
        }
        else
        {
            pre = now;
            now = now->next;
        }
    }
    return list;
}
void out(List* head)
{
    List* now = head;
    while (now != NULL)
    {
        printf("%d\t", now->data);
        now = now->next;
    }
    return;
}
int main() {
    int i;
    
    List* head1 = (List*)malloc(sizeof(List));
    if (head1 == NULL)
        exit(1);
    List* head_temp = head1;
    int data[8] = { 1,1,1,1,5,5,5,10 };
    for (i = 0; i < 7; i++) {
        head_temp->data = data[i];
        List* next = (List*)malloc(sizeof(List));
        if (next == NULL)
            exit(1);
        head_temp->next = next;
        head_temp = next;
    }
    head_temp->data = data[i];
    head_temp->next = NULL;
    out(remove_repetition(head1));
}

(Solutions tested in MVSC 2019 16.8.4) A trick to avoid the C6001. MVSC likes to check the size of the array pointed to by the pointer to be greater than 0. Below this check is added and warning C6001 disappears.

For other cases of writing code, except for your case, but related to С6001 and free (), it is necessary to check the loop counter to be greater than zero. For example,

if(supremum < 1){/ handling /}

should be performed for

for(counter = 0; counter < supremum; counter++){ //free}.

(I do not give a real example due to its complexity.) In other words, C6001 and C6386 are excluded by the same mechanism. (See Any way to avoid warning C6386, without disabling it or Code Analysis altogether (comment by Costantino Grana) ).

Anyway, my solution to your problem should be verified by experienced programmers.

#include <stdio.h>
#include <stdlib.h>
typedef struct list List;
struct list
{
    int data;
    List* next;
};
List* remove_repetition(List* list)
{
    //The only addition is here.
    if (_msize(list->next) == 0) { /*handling*/ }
    
    List* pre = list;
    if (pre == NULL)
        return NULL;
    List* now = list->next;
    while (now != NULL)
    {
        if (pre->data == now->data)//the warning occurs here!
        {
            pre->next = now->next;
            List* del = now;
            now = now->next;
            free(del);
        }
        else
        {
            pre = now;
            now = now->next;
        }
    }
    return list;
}
void out(List* head)
{
    List* now = head;
    while (now != NULL)
    {
        printf("%d\t", now->data);
        now = now->next;
    }
    return;
}
int main() {
    int i;
    
    List* head1 = (List*)malloc(sizeof(List));
    if (head1 == NULL)
        exit(1);
    List* head_temp = head1;
    int data[8] = { 1,1,1,1,5,5,5,10 };
    for (i = 0; i < 7; i++) {
        head_temp->data = data[i];
        List* next = (List*)malloc(sizeof(List));
        if (next == NULL)
            exit(1);
        head_temp->next = next;
        head_temp = next;
    }
    head_temp->data = data[i];
    head_temp->next = NULL;
    out(remove_repetition(head1));
}

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