Valgrind给出错误:"条件跳转或移动取决于未初始化的值"

时间:2014-03-23 23:49:17

标签: c pointers valgrind

我很确定我知道这个错误意味着什么。这是非常自我解释的。但我不明白为什么。特别是因为错误点只有59和61号线。与函数printDictionarymenu一样。我不明白为什么它会在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; } 采用与参数相同的变量。有什么建议吗?

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;
}

这是我的loadDictionary函数。它确实初始化了数组!此外,我只是尝试在loadDicitonary之前添加addign函数,并在它之前使用数组作为参数的任何函数。所以问题显然在于loadDictionary。

==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)

Valgrind Log。

{{1}}

1 个答案:

答案 0 :(得分:4)

上面的Valgrind消息表明已经使用了程序中未初始化变量的某个位置。

要查看有关程序中未初始化数据来源的信息,请使用选项:

  

- 轨迹 - 起源= YES

这可以帮助您找出程序中未初始化的值错误的根本原因。