valgrind在运行比较时显示错误(valgrind shows an error when running a comparison)
我有以下功能
#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)) // ... }I have the following function
#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 } }When I run it with a pointer to an initialized struct block and NULL, valgrind shows this error (line 6 is the first comparison):
==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)EDIT: Here is the call of 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)) // ... }最满意答案
实际上,该值未初始化,因此可以指向任何内容; 我不知道调试器告诉我它是什么0x0。 我将以不同的方式编写测试(例如,测试地址)。
Actually, the value is not initialized, so this can point to anything; I don't know what the debugger does tell me it's 0x0. I will write my tests differently (testing the address instead, for example).
更多推荐
发布评论