条件跳转或移动取决于使用valgrind的未初始化值

时间:2014-11-08 19:11:23

标签: c valgrind

我一直在尝试修复程序中的内存泄漏。这是导致错误的代码段:

unsigned long me_hash(MEntry *me, unsigned long size) {

    unsigned long hash;
    unsigned long hash_init;
    int i;

    hash = 0;
    hash_init = 0;

    for (i = 0; me -> surname[hash_init] != '\0'; i++) {
        hash_init += (int) me -> surname[i];
    }

    hash += 5381*hash_init;

    for (i = 0, hash_init = 0; me -> postcode[hash_init] != '\0'; i++) {
        hash_init += (int) me -> postcode[i];
    }

    hash_init = hash_init + me -> house_number;

    hash += 5381*hash_init;

    hash = hash%size;

    return hash;
}

当我运行Valgrind时,这是我的终端上打印的结果:

==7853== Conditional jump or move depends on uninitialised value(s)
==7853==    at 0x40116D: me_hash (in /home/iva/University/AdvProg/Assessed/a.out)
==7853==    by 0x400D7A: ml_lookup (in /home/iva/University/AdvProg/Assessed/a.out)
==7853==    by 0x4013CD: main (in /home/iva/University/AdvProg/Assessed/a.out)

对此的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:1)

for (i = 0; me -> surname[hash_init] != '\0'; i++) {
    hash_init += (int) me -> surname[i];
}

上面有一个糟糕的循环条件。应该是:

for (i = 0; me -> surname[i]; i++) {
    hash_init += (int) me -> surname[i];
}

第二个循环的问题相同。