valgrind在运行比较时显示错误

时间:2012-06-02 19:05:41

标签: c valgrind

我有以下功能

#include <stdio.h>
#include <stdlib.h>
#include "block.h"

bool block_equal(const struct block *block1, const struct block *block2) {
    if ((block1 == NULL) ^ (block2 == NULL))
        return false;
    else if (block1 == NULL & block2 == NULL)
        return true;
    else {
        // ... Some stuff
    }
}

当我使用指向初始化struct blockNULL的指针运行它时,valgrind显示此错误(第6行是第一次比较):

==24444== Conditional jump or move depends on uninitialised value(s)
==24444==    at 0x402E34: block_equal (block.c:6)
==24444==    by 0x4025D3: test_chunks_write (test_chunks.c:22)
==24444==    by 0x4E31FE8: ??? (in /usr/lib/libcunit.so.1.0.1)
==24444==    by 0x4E323C6: ??? (in /usr/lib/libcunit.so.1.0.1)
==24444==    by 0x4E326E7: CU_run_all_tests (in /usr/lib/libcunit.so.1.0.1)
==24444==    by 0x401627: main (tests.c:15)

编辑:这是block_equal的召唤:

#include <stdlib.h>
#include <CUnit/CUnit.h>
#include "../chunks.h"
#include "../block.h"

void test_chunks_write(void) {
    struct block *block1, *block2;
    block1 = malloc(sizeof(struct block));
    block2 = malloc(sizeof(struct block));
    block1->type = BLOCK_WOOD;
    block2->type = BLOCK_STONE;
    chunks *chunks = chunks_empty();
    coordinates coord1;
    coordinates coord2;
    for (int i=0; i<SPACE_DIMENSION; i++) {
        coord1[i] = i+1;
        coord2[i] = 2*i+5;
    }
    chunks_write_data(&chunks, coord1, block1);
    CU_ASSERT(block_equal(block1, chunks_select_data(chunks, coord1)))
    CU_ASSERT_FALSE(block_equal(block2, chunks_select_data(chunks, coord1)))
    struct block* block3 = chunks_select_data(chunks, coord2);

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // According to the debugger, at this point block2 is 0x605600 and block3 is 0x0
    CU_ASSERT_FALSE(block_equal(block2, block3))
    // ...
}

1 个答案:

答案 0 :(得分:0)

实际上,该值未初始化,因此可以指向任何内容;我不知道调试器告诉我它是什么0x0。我将以不同的方式编写测试(例如,测试地址)。