c - Valgrind:未初始化的值是由堆分配创建的

标签 c malloc valgrind

我遇到了一些有关统一值和条件跳转的 Valgrind 错误。这是我的 Valgrind 输出

==28124== Conditional jump or move depends on uninitialised value(s)
==28124==    at 0x4C2E0E9: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28124==    by 0x400AA7: append_character (in /home/i)
==28124==    by 0x401319: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)
==28124==  Uninitialised value was created by a heap allocation
==28124==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28124==    by 0x4012C0: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)
==28124== 
==28124== Conditional jump or move depends on uninitialised value(s)
==28124==    at 0x4C2E0E9: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28124==    by 0x400AA7: append_character (in /home/)
==28124==    by 0x40134F: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)
==28124==  Uninitialised value was created by a heap allocation
==28124==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28124==    by 0x4012E0: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)
==28124== 
==28124== Conditional jump or move depends on uninitialised value(s)
==28124==    at 0x400987: binary_decimal (in /home/)
==28124==    by 0x401377: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)
==28124==  Uninitialised value was created by a heap allocation
==28124==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28124==    by 0x4012E0: refresh_address (in /home/)
==28124==    by 0x402067: main (in /home/)

这是我的“append_character”函数。非常简单的东西。

void append_character(char* str, char ch){
    int len = strlen(str) + 1;
    str[len] = ch;
    str[len + 1] = '\0';
}

这是我的“refresh_address”函数。问题似乎出现的地方。

void refresh_address(int memLength, address_info *mem, char *address){
     int j = 0;
     mem -> numSetIndexBits = calculate_set_index_bits();
     mem -> numBlockOffsetBits = calculate_block_offset_bits();
     mem -> numTagBits = calculate_num_tag_bits(memLength, mem);
     mem -> tag = malloc(mem -> numSetIndexBits * sizeof(char) + 1);
     mem -> setIndex = malloc(mem -> numSetIndexBits * sizeof(char) + 1);

     for(j = 0; j < mem -> numTagBits; ++j){
        append_character(mem -> tag, address[j]);
     }

     while (j < (mem -> numSetIndexBits + mem -> numTagBits)) {
        append_character(mem -> setIndex, address[j]);
        j++;
     }    
     mem -> decimalIndex = binary_decimal(mem -> setIndex);
}

我想不出我做错了什么。知道是什么原因造成的吗?感谢您的帮助!

编辑:address_info *mem 在 main 中使用以下代码初始化,其中 address_info 是一个结构。

 while(fgets(buffer, 130, stdin)){
    if(sscanf(buffer, "%c:%d:%d", &accessTypes[i], &accessSize[i], &address[i]) != EOF) {
        memory = malloc(sizeof(address_info));
        init_address_info(memory);

        if (accessTypes[i] == 'W') {
                memory -> accessType = "W";
        }
        else {
                memory -> accessType = "R";
        }
        binary_add = binary_address(address[i]);
        mem_length = strlen(binary_add);
        memory -> numSetIndexBits = calculate_set_index_bits();
        refresh_address(mem_length, memory, binary_add); /*Calls malloc for the memory's set index and tag in this function*/
        ++i;
        free(binary_add);
        free(memory);
    }
}

最佳答案

我突然想到的一件明显的事情是,您为 mem->tag 分配了内存,然后立即开始向其添加内容……但您从未初始化过它(听起来很熟悉?) .这意味着您在传入的 char * 上调用 strlen(),它引用 mem->tag,但它是未初始化。这只是自找麻烦,很现实的是,您的 strlen 调用和随后的字符追加,然后是 null 将超出您在堆外的分配。

关于c - Valgrind:未初始化的值是由堆分配创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34101845/

相关文章:

c++ - 如何解决据报告驻留在 STL 字符串中的内存泄漏?

c - 我如何在 C 中通过 malloc 分配一个指针数组和一个二维数组?

c - 如何检查内存是否可用于读/写操作?

c - valgrind 的段错误

c++ - 更小的 GCC 包,只需要 C

c - Luajit FFI 直接取消引用不需要转换的值的语义

c++ - C++ 中 and() 函数的用途

c - C 中的点或 "reset"变量

c - 分配时大小读/写无效

c++ - 如何解释此错误,堆摘要,未初始化的变量?