简体   繁体   English

为什么VS2013抱怨“使用未初始化的内存”?

[英]Why is VS2013 complaining about “using uninitialized memory”?

I have a code that goes like this: 我有一个代码如下:

#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's Code Analysis is complaining that I am: VS的代码分析抱怨我是:

"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

I mean, it's a pointer and I'm checking to see if it is NULL . 我的意思是,它是一个指针,我正在检查它是否为NULL How will I ever know if *p is initialized? 我怎么知道* p是否被初始化?

Oddly enough, if there's only 1 other pointer inside the struct -- only char *a , for example -- the warning doesn't trigger. 奇怪的是,如果结构中只有一个其他指针 - 例如,只有char *a - 警告不会触发。 It also doesn't show up if I do free(p->b) before free(p->a) (swap lines 12 and 13). 如果我在free(p->b)之前free(p->a) free(p->b) free(p->a) (交换行12和13),它也不会出现。

It seems to be a problem with the analyzer tool of visual studio 2013 这似乎是visual studio 2013的分析工具的一个问题

as explained here: 如下所述:

https://randomascii.wordpress.com/2011/07/25/analyze-for-visual-studiothe-ugly-part-1/ 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/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/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/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/ https://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/

as an update in the part 5, we can read this: 作为第5部分的更新,我们可以读到:

Update: Luckily VC++ 2013 has solved many of these issues, but the problems with __analysis_assume remain. 更新:幸运的是VC ++ 2013解决了许多这些问题,但__analysis_assume的问题仍然存在。

So even if they solved many of theses warning issues with newest visual studio versions, still some bugs occurs in the analyzer tool. 因此,即使他们用最新的Visual Studio版本解决了许多这些警告问题,分析器工具中仍会出现一些错误。

Test with VS2015 Enterprise: gives the same problem 使用VS2015 Enterprise进行测试:出现同样的问题

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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