1

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));
}

marass
  • 35
  • 4
  • The code seems fine and `gcc -Wall -Wextra -pedantic` doesn't give any warning. Looks like a false warning. – Support Ukraine Jan 18 '21 at 20:41
  • See https://stackoverflow.com/questions/33513003/why-is-vs2013-complaining-about-using-uninitialized-memory – Support Ukraine Jan 18 '21 at 20:41
  • That looks like a false positive. Yet it vanishes if you replace malloc/free with new/delete and add a constructor that initializes data and next in `list`. – Werner Henze Jan 18 '21 at 20:41
  • Also try https://www.google.com/search?q=c6001+using+uninitialized+memory+site:stackoverflow.com which will lead you to other similar questions – Support Ukraine Jan 18 '21 at 20:42
  • 1
    Here: https://stackoverflow.com/questions/59238295/unlogical-c6001-warning-using-uninitialized-memory-warning-in-c-with-visual-stu there is a suggestion to try `calloc` instead of `malloc` to get rid of the false warning. Not sure it will work for you... – Support Ukraine Jan 18 '21 at 20:44
  • 1
    @WernerHenze The question is tagged with C, not C++. – the busybee Jan 19 '21 at 07:09
  • 1
    Microsoft's compilers are known to be ... uhm ... not so good with C. Use a standard compliant compiler. – the busybee Jan 19 '21 at 07:12

1 Answers1

2

(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));
}
diversiter
  • 21
  • 4