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

我有一个代码如下:

#include  #include  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的代码分析抱怨我是:

 "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是否被初始化?

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

这似乎是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部分的更新,我们可以读到:

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

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

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

在此处输入图像描述