未初始化的值由堆栈分配和其他错误valgrind创建

时间:2015-03-19 02:46:49

标签: c memory valgrind

首先,我查看过其他类似的问题但他们并没有帮助我。

我有以下Valgrind输出:

==3795== Memcheck, a memory error detector
==3795== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3795== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3795== Command: ./bin/listTest /home/pi/cis2500/A4/assets/diary.txt
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C8098: _IO_file_fopen@@GLIBC_2.4 (fileops.c:296)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C809C: _IO_file_fopen@@GLIBC_2.4 (fileops.c:296)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C80A4: _IO_file_fopen@@GLIBC_2.4 (fileops.c:296)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C80A8: _IO_file_fopen@@GLIBC_2.4 (fileops.c:296)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C80B0: _IO_file_fopen@@GLIBC_2.4 (fileops.c:296)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Syscall param open(flags) contains uninitialised byte(s)
==3795==    at 0x491A00C: open (syscall-template.S:82)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C7F20: _IO_file_open (fileops.c:240)
==3795==    by 0x48C80E3: _IO_file_fopen@@GLIBC_2.4 (fileops.c:336)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Use of uninitialised value of size 4
==3795==    at 0x483920C: strstr (in /usr/lib/valgrind/vgpreload_memcheck-     arm-linux.so)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Use of uninitialised value of size 4
==3795==    at 0x4839230: strstr (in /usr/lib/valgrind/vgpreload_memcheck-    arm-linux.    so)
==3795==      Uninitialised value was created by a stack allocation
==3795==        at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x4839238: strstr (in /usr/lib/valgrind/vgpreload_memcheck-   arm-linux.so)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x4839228: strstr (in /usr/lib/valgrind/vgpreload_memcheck-arm-linux.so)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C8100: _IO_file_fopen@@GLIBC_2.4 (fileops.c:345)
==3795==    by 0x48BBEF3: __fopen_internal (iofopen.c:93)
==3795==    by 0x8D77: openFile (playList.c:6)
==3795==    by 0x87B3: main (main.c:18)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C8580: _IO_file_underflow@@GLIBC_2.4 (fileops.c:553)
==3795==    by 0x48C99A7: _IO_default_uflow (genops.c:440)
==3795==    by 0x48C9853: __uflow (genops.c:394)
==3795==    by 0x48BD023: _IO_getline_info (iogetline.c:74)
==3795==    by 0x48BCF6F: _IO_getline (iogetline.c:42)
==3795==    by 0x48BBCBF: fgets (iofgets.c:58)
==3795==    by 0x8863: main (main.c:20)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
pear (fruit):1.40[h]
cheese (dairy):13.40[h]
gum (fat):0.60[j]
==3795== Conditional jump or move depends on uninitialised value(s)
==3795==    at 0x48C7CFC: _IO_file_close_it@@GLIBC_2.4 (fileops.c:168)
==3795==    by 0x48BB40B: fclose@@GLIBC_2.4 (iofclose.c:62)
==3795==    by 0x8887: main (main.c:35)
==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)
==3795== 
==3795== 
==3795== HEAP SUMMARY:
==3795==     in use at exit: 0 bytes in 0 blocks
==3795==   total heap usage: 13 allocs, 13 frees, 914 bytes allocated
==3795== 
==3795== All heap blocks were freed -- no leaks are possible
==3795== 
==3795== For counts of detected and suppressed errors, rerun with: -v
==3795== ERROR SUMMARY: 26 errors from 14 contexts (suppressed: 13 from 6)

以下是main.c

FILE *fp = NULL;
Food *node = NULL;
Food *head = NULL;
char buffer[150] = {0};
char * name = {0};
char * group = {0};
double calories = {0};
char theType = {0};

checkNumArgs(argc, 2);

fp = openFile((char*)argv[1], 'r');

while (fgets(buffer, 150, fp) != NULL) {
    name = strtok(buffer, ",");
    group = strtok(NULL, ",");
    calories = atof(strtok(NULL, ","));
    theType = *strtok(NULL, ",");
    node = createRecord(name, group, calories, theType);
    head = addToList(head, node); 
}
printList(head);
destroyList(head);
fclose(fp);
return 0;

以下是playList.c

FILE *openFile(char *filePath, char mode) {
    FILE *fp = NULL;

    fp = fopen(filePath, &mode);

    if (fp == NULL) {
        perror("Could not open file");
        exit(0);
    }

    return fp;
}

Food *addToList(Food *head, Food *node) {
    if (node->type == 'h') {
        head = addToFront(head, node);
    } else if (node->type == 'j') {
        head = addToBack(head, node);
    }

    return head;
}

我的主要问题是

==3795==  Uninitialised value was created by a stack allocation
==3795==    at 0x8D4C: openFile (playList.c:3)

Conditional jump or move depends on uninitialised value(s)

我的所有变量都已初始化,所以我不明白为什么Valgrind会对我大喊大叫。

0 个答案:

没有答案