我得到这个 Valgrind 输出(这是我得到的唯一错误):
==20627== Conditional jump or move depends on uninitialised value(s)
==20627== at 0x804913A: main (main.c:223)
我的main.c
大致是这样的:
//other code
char **sets;
//other code
//char** get_char_sets(FILE *source);
sets = get_char_sets(config_file); // I malloc the sets in here
//other code
int i = 0;
while(sets[i]){ // line 223
free(sets[i]);
i++;
}
free(sets);
//other code
get_char_sets
看起来像这样:
char** get_char_sets(FILE *source){
char **sets = malloc((n + 1) * sizeof(char*));
for(int i=0;i<=n;i++){
sets[i] = malloc(1 * sizeof(char));
}
//rest of function
return sets;
}
我知道 valgrind 说我正在使用一个未初始化的变量,我唯一能看到的是sets
,但它被malloc()
编辑get_char_sets()
并分配了返回的指针。
我怎样才能摆脱那个 valgrind 错误以更正我的代码?