问题描述
我有一个这样的代码:
#include #include 类型定义结构{字符 *a;字符*b;国际 c;} 我的风格;void free_my_type(my_type *p) {如果 (p) {如果 (p->a) 自由 (p->a);//第 12 行如果(p-> b)免费(p-> b);//第 13 行免费(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);返回0;}
VS 的代码分析抱怨我是:
"C6001 使用未初始化的内存 '*p'"'*p' 未初始化 12跳过这个分支,(假设 'p->b' 是假的) 13'*p' 已使用,但可能尚未初始化 13
我的意思是,它是一个指针,我正在检查它是否为 NULL
.我怎么知道 *p 是否已初始化?
奇怪的是,如果结构中只有 1 个其他指针——例如,只有 char *a
——警告不会触发.如果我在 free(p->a)
之前执行 free(p->b)
(交换第 12 和 13 行),它也不会显示.
好像是visual studio 2013的analyzer工具的问题
如下所述:
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:
"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
. How will I ever know if *p is initialized?
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).
It seems to be a problem with the analyzer tool of 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/29/analyze-for-visual-studiothe-ugly-part-2/
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:
So even if they solved many of theses warning issues with newest visual studio versions, still some bugs occurs in the analyzer tool.
Test with VS2015 Enterprise: gives the same problem
这篇关于为什么 VS2013 会抱怨“使用未初始化的内存"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!