【发布时间】:2014-03-23 23:49:16
【问题描述】:
我很确定我知道这个错误意味着什么。这很不言自明。但我不明白为什么。特别是因为错误仅指向第 59 行和第 61 行。就像函数 printDictionary 和 menu 一样。我不明白为什么它会在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 之前尝试了 addign 函数,并且在它之前将数组作为参数的任何函数都有效。所以问题显然出在 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;
}
Valgrind 日志。
==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)
【问题讨论】:
-
loadDictionary是如何工作的?我的猜测是有一个代码路径返回0,但没有设置wordArray。 -
第 59 行和第 61 行分别是哪几行?
-
第 59 行 -> printDictionary(wordArray, wordCount);第 61 行 -> 菜单(&wordArray, &wordCount, argv[2]);
-
我没有看到有条件的跳转或移动这些调用中的任何一个。你能发布完整、准确的错误信息吗?
-
错误中的行号与源代码不匹配。请重新编译并再次运行
valgrind。 (另外,您还没有将源代码包含到menu()函数中。)