【问题标题】:a warning in c :C6001 Using uninitialized memory '*now'c 中的警告:C6001 使用未初始化的内存 '*now'
【发布时间】:2021-01-18 20:30:27
【问题描述】:

我不知道为什么会出现这个警告 我试图写一个代码来删除链表中的重复数据 我意识到的一点是,当我评论包含免费命令的行时,警告不会发生! 我在 Visual Studio 2019 编译器中收到了这个警告 请告诉我它的用途以及如何解决此问题

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

【问题讨论】:

  • 代码看起来很好,gcc -Wall -Wextra -pedantic 没有给出任何警告。看起来像是一个错误的警告。
  • 这里:stackoverflow.com/questions/59238295/… 建议尝试使用calloc 而不是malloc 来消除错误警告。不确定它是否适合您...
  • @WernerHenze 这个问题用 C 标记,而不是 C++。
  • 众所周知,Microsoft 的编译器......嗯......不太适合 C。使用符合标准的编译器。

标签: c memory initialization warnings


【解决方案1】:

(在 MVSC 2019 16.8.4 中测试的解决方案) 避免 C6001 的技巧。 MVSC喜欢检查指针指向的数组的大小是否大于0。在这个检查的下方添加了这个检查,警告C6001消失了。

对于其他写代码的情况,除了你的情况,但是和С6001和free()有关,需要检查循环计数器是否大于零。例如,

if(supremum 处理/}

应该为

for(counter = 0;counter

(由于其复杂性,我没有给出一个真实的例子。)换句话说,C6001和C6386被相同的机制排除在外。 (参见Any way to avoid warning C6386, without disabling it or Code Analysis altogether(Costantino Grana 评论)。

无论如何,我对你问题的解决方案应该由有经验的程序员验证。

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

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多