我有以下功能
#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 block
,NULL
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))
// ...
}