为什么VS2013抱怨“使用未初始化的内存"? [英] Why is VS2013 complaining about "using uninitialized memory"?

查看:1904
本文介绍了为什么VS2013抱怨“使用未初始化的内存"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *a;
    char *b;
    int c;
} my_type;

void free_my_type(my_type *p) {
    if (p) {
        if (p->a) free(p->a);  // line 12
        if (p->b) free(p->b);  // line 13
        free(p);
    }
}

int main(void) {
    my_type *p = malloc(sizeof(*p));

    p->a = malloc(10);
    p->b = malloc(10);
    p->c = 10;

    free_my_type(p);

    return 0;
}

VS的代码分析抱怨我是:

VS's Code Analysis is complaining that I am:

"C6001 Using uninitialized memory '*p'"

        '*p' is not initialized                             12
        Skip this branch, (assume 'p->b' is false)          13
        '*p' is used, but may not have been initialized     13

我的意思是,它是一个指针,我正在检查它是否为NULL.我怎么知道* p是否已初始化?

I mean, it's a pointer and I'm checking to see if it is NULL. How will I ever know if *p is initialized?

奇怪的是,如果结构中只有1个其他指针(例如,只有char *a),则不会触发警告.如果我在free(p->a)之前执行free(p->b)(交换第12和13行),也不会显示.

Oddly enough, if there's only 1 other pointer inside the struct -- only char *a, for example -- the warning doesn't trigger. It also doesn't show up if I do free(p->b) before free(p->a) (swap lines 12 and 13).

推荐答案

Visual Studio 2013的分析器工具似乎有问题

It seems to be a problem with the analyzer tool of visual studio 2013

如此处所述:

https://randomascii.wordpress.com/2011/07/25/analyze-for-visual-studiothe-ugly-part-1/

https://randomascii.wordpress.com/2011/07/29/analyze-for-visual-studiothe-ugly-part-2/

https://randomascii.wordpress.com/2011/08/06/analyze-for-visual-studiothe-ugly-part-3-false-positives/

https://randomascii.wordpress.com/2011/08/20/analyze-for-visual-studiothe-ugly-part-4-false-negatives/

https: //randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/

作为第5部分的更新,我们可以阅读以下内容:

as an update in the part 5, we can read this:

更新:幸运的是VC ++ 2013解决了许多此类问题,但__analysis_assume的问题仍然存在.

因此,即使他们使用最新的Visual Studio版本解决了许多此类警告问题,分析器工具仍会出现一些错误.

So even if they solved many of theses warning issues with newest visual studio versions, still some bugs occurs in the analyzer tool.

使用VS2015 Enterprise进行测试:出现相同的问题

Test with VS2015 Enterprise: gives the same problem

这篇关于为什么VS2013抱怨“使用未初始化的内存"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆