Valgrind:未初始化的值是由堆分配创建的 [英] Valgrind: Uninitialised value was created by a heap allocation

查看:74
本文介绍了Valgrind:未初始化的值是由堆分配创建的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting a few Valgrind errors concerning Unitialised values and Conditional Jumps. Here is my Valgrind output

==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'函数.很简单的东西.

Here is my 'append_character' function. Pretty simple stuff.

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

这是我的"refresh_address"功能.问题似乎出现的地方.

And here is my 'refresh_address' function. Where the problems seem to arise.

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);
}

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

I can't think of what I'm doing wrong. Any idea what is causing this? Thanks for the help!

address_info *mem 在 main 中使用以下代码初始化,其中 address_info 是一个结构体.

address_info *mem is initialized in main with the following code where address_info is a struct.

 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将会超出堆的分配范围,这是非常现实的.

One obvious thing that jumps out at me is that you allocate memory for mem->tag and then immediately start appending things to it... but you never ever initialized it (sound familiar?). This means that you are calling strlen() on the char * that you pass in, which refers to mem->tag, but it is uninitialized. That's just asking for trouble and it is very realistic that your strlen call and subsequent append of the character and then a null will exceed your allocation off of the heap.

这篇关于Valgrind:未初始化的值是由堆分配创建的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆