C Valgrind给出错误:“;条件跳转或移动取决于未初始化的值;
我很确定我知道这个错误的意思。这是不言自明的。但我不明白为什么。特别是因为错误只指向第59行和第61行。与功能C Valgrind给出错误:“;条件跳转或移动取决于未初始化的值;,c,pointers,valgrind,C,Pointers,Valgrind,我很确定我知道这个错误的意思。这是不言自明的。但我不明白为什么。特别是因为错误只指向第59行和第61行。与功能打印字典和菜单相同。我不明白为什么它会在loadDictionary上工作,它使用相同的变量作为参数。有什么建议吗 int main(int argc, char* argv[]) { char **wordArray; /* declare the dynamic array of strings, not yet allocated.*/ int capacit
打印字典
和菜单
相同。我不明白为什么它会在loadDictionary
上工作,它使用相同的变量作为参数。有什么建议吗
int main(int argc, char* argv[])
{
char **wordArray; /* declare the dynamic array of strings, not yet allocated.*/
int capacity = INITIAL_ARRAY_MAX;
int wordCount = 0;
if (argc != 3)
{
fprintf(stderr,"Usage: %s inputfile outputfile\n", argv[0]);
return 1;
}
if (loadDictionary(argv[1], &wordArray, &wordCount, &capacity) != 0)
{
fprintf(stderr," loadDictionary failed! Program terminating...\n");
return 1; /* already in main() so exit unneccesary.*/
}
printf("\n Finished loading %d words from dictionary.\n", wordCount);
printDictionary(wordArray, wordCount); /* Line 59 */
menu(&wordArray, &wordCount, argv[2]); /* Line 61 */
return 0;
}
这是我的loadDictionary函数。它确实初始化了数组!此外,我还尝试在loadDicitonary之前添加函数,以及它之前的任何函数,该函数以数组作为参数。因此,问题显然在于loadDictionary
int loadDictionary(char *inputFileName, char ***array, int *count, int *capacity)
{
FILE *inputFile;
char word[WORD_LENGTH];
if ((inputFile = fopen(inputFileName, "r")) == NULL)
{
fprintf(stderr,"Error opening input file, %s\n", inputFileName);
return -1;
}
*array = (char **)malloc(*capacity * sizeof(char*));
if (*array == NULL)
{
fprintf(stderr, "Malloc of array in loadDictionary failed!\n");
return -1;
}
printf("Reading file %s (each . is 5000 words read)\n", inputFileName);
*count = 0;
while (fscanf(inputFile, "%s", word) == 1)
{
if (*count >= *capacity)
{
/*makes new array of double size with contents of original array.*/
if (doubleArraySize(array, *count, capacity) != 0){
fprintf(stderr, "Doubling array failed!\n");
}
}
if (insertWord(*array, count, word) != 0)
{
fprintf(stderr," Insert returned an error!\n");
fclose(inputFile);
return 1;
}
if (*count % 5000 == 0)
{
printf(".");
fflush(stdout);
}
}
fclose(inputFile);
return 0;
}
伐木场
==6107== Memcheck, a memory error detector
==6107== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6107== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6107== Command: ./lab2 105-words.txt output.txt
==6107== Parent PID: 3994
==6107==
==6107== Conditional jump or move depends on uninitialised value(s)
==6107== at 0x4089E29: vfprintf (vfprintf.c:1630)
==6107== by 0x4091EFE: printf (printf.c:35)
==6107== by 0x804882F: main (lab2.c:59)
==6107==
==6107== Conditional jump or move depends on uninitialised value(s)
==6107== at 0x4089E29: vfprintf (vfprintf.c:1630)
==6107== by 0x4091EBE: fprintf (fprintf.c:33)
==6107== by 0x8048F49: menu (lab2.c:271)
==6107== by 0x804884F: main (lab2.c:61)
==6107==
==6107==
==6107== HEAP SUMMARY:
==6107== in use at exit: 0 bytes in 0 blocks
==6107== total heap usage: 110 allocs, 110 frees, 3,197 bytes allocated
==6107==
==6107== All heap blocks were freed -- no leaks are possible
==6107==
==6107== For counts of detected and suppressed errors, rerun with: -v
==6107== Use --track-origins=yes to see where uninitialised values come from
==6107== ERROR SUMMARY: 210 errors from 2 contexts (suppressed: 0 from 0)
上面的消息表示程序中的某个地方使用了未初始化的变量
要查看程序中未初始化数据源的信息,请使用以下选项:
--轨迹原点=是
这将有助于您追踪程序中未初始化值错误的根本原因。上述消息表示程序中的某个未初始化变量已被使用
要查看程序中未初始化数据源的信息,请使用以下选项:
--轨迹原点=是
这将有助于您追踪程序中未初始化值错误的根本原因。加载字典如何工作?我猜有一个代码路径返回
0
,但没有设置wordArray
。哪些行是59和61?第59行->打印字典(wordArray,wordCount);第61行->菜单(&wordArray,&wordCount,argv[2]);在这两个调用中,我都没有看到有条件的跳转或移动。您能发布完整、准确的错误消息吗?错误中的行号与源代码不匹配。请重新编译并再次运行valgrind
。(另外,您还没有将源代码包含到菜单()
函数中。)loadDictionary
是如何工作的?我猜有一个代码路径返回0
,但没有设置wordArray
。哪些行是59和61?第59行->打印字典(wordArray,wordCount);第61行->菜单(&wordArray,&wordCount,argv[2]);在这两个调用中,我都没有看到有条件的跳转或移动。您能发布完整、准确的错误消息吗?错误中的行号与源代码不匹配。请重新编译并再次运行valgrind
。(另外,您还没有将源代码包含到菜单()
函数中。)