分段故障恢复哈希表值

时间:2019-04-26 14:30:32

标签: c data-structures hashtable valgrind

最后一个问题Pointer problem with HashTable insertion and lookup之后 我用Valgrind执行了我的代码。首先,我正确地恢复了插入的值,然后在第二次尝试中出现“分段错误”。为什么会发生这种情况以及我要尝试访问的内容?

代码

#include "list.h"
#include "hash-table.h"
#include <stdio.h>

//typedef unsigned int (*HashTableHashFunc)(HashTableKey value);
unsigned int hashFunc(HashTableKey v_key)
{
    unsigned int *key = (unsigned int *)v_key;
    return *key % 20;
}

//typedef int (*HashTableEqualFunc)(HashTableKey value1, HashTableKey value2);
int equalFunc(HashTableKey value1, HashTableKey value2)
{
    int *key1 = (int *)value1;
    int *key2 = (int *)value2;
    return *key1 == *key2;
}

int main(int argc, char const *argv[])
{
    HashTable *mapMatrices;
    //ListEntry *posicionListaPeticiones;

    mapMatrices = hash_table_new(hashFunc, equalFunc);

    for (int i = 0; i < 10; i++)
    {
        int value = i * 200;
        int stat = hash_table_insert(mapMatrices, &i, &value);
        if (!stat)
            printf("Error inserting key %i with value %i\n", &i, value);
        else
        {
            printf("Inserted key %i with value %i\n", i, value);
            void *v_value = hash_table_lookup(mapMatrices, &i);
            int value = *(int *)v_value;
            printf("Key %i : Value %i : Void pointer %x : Value pointer %x\n", i, value, v_value, &value);
        }
    }

    for (int i = 0; i < 10; i++)
    {
        void *v_value = hash_table_lookup(mapMatrices, &i);
        int value = *(int *)v_value;
        printf("Key %i : Value %i : Void pointer %x : Value pointer %x\n", i, value, v_value, &value);
    }
}

Valgrind输出

==13069== Memcheck, a memory error detector
==13069== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13069== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13069== Command: ./main
==13069== 
Inserted key 0 with value 0
Key 0 : Value 0 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 1 with value 200
Key 1 : Value 200 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 2 with value 400
Key 2 : Value 400 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 3 with value 600
Key 3 : Value 600 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 4 with value 800
Key 4 : Value 800 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 5 with value 1000
Key 5 : Value 1000 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 6 with value 1200
Key 6 : Value 1200 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 7 with value 1400
Key 7 : Value 1400 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 8 with value 1600
Key 8 : Value 1600 : Void pointer fefffd04 : Value pointer fefffd08
Inserted key 9 with value 1800
Key 9 : Value 1800 : Void pointer fefffd04 : Value pointer fefffd08
==13069== Invalid read of size 4
==13069==    at 0x108910: main (main.c:45)
==13069==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==13069== 
==13069== 
==13069== Process terminating with default action of signal 11 (SIGSEGV)
==13069==  Access not within mapped region at address 0x0
==13069==    at 0x108910: main (main.c:45)
==13069==  If you believe this happened as a result of a stack
==13069==  overflow in your program's main thread (unlikely but
==13069==  possible), you can try to increase the size of the
==13069==  main thread stack using the --main-stacksize= flag.
==13069==  The main thread stack size used in this run was 8388608.
==13069== 
==13069== HEAP SUMMARY:
==13069==     in use at exit: 1,840 bytes in 12 blocks
==13069==   total heap usage: 13 allocs, 1 frees, 2,864 bytes allocated
==13069== 
==13069== LEAK SUMMARY:
==13069==    definitely lost: 0 bytes in 0 blocks
==13069==    indirectly lost: 0 bytes in 0 blocks
==13069==      possibly lost: 0 bytes in 0 blocks
==13069==    still reachable: 1,840 bytes in 12 blocks
==13069==         suppressed: 0 bytes in 0 blocks
==13069== Rerun with --leak-check=full to see details of leaked memory
==13069== 
==13069== For counts of detected and suppressed errors, rerun with: -v
==13069== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

0 个答案:

没有答案