HashTable插入和查找的指针问题

时间:2019-04-25 19:34:25

标签: c hashtable

我正在使用来自github的数据结构的自定义C实现。这里的文档:http://fragglet.github.io/c-algorithms/doc/

这是我第一次使用这种低级别的HashTable,并且在恢复以前插入的值时遇到一些指针问题。

我的代码只是一个简单的测试,仅具有插入和恢复数据所必需的内容。

#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 key = i;
        int value = i * 200;
        int stat = hash_table_insert(mapMatrices, &key, &value);
        if (!stat)
            printf("Error inserting key %i with value %i\n", key, value);
        else
            printf("Inserted key %i with value %i\n", key, value);
    }

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

如果我打印数据地址,这是我的输出

Inserted key 0 with value 0
Inserted key 1 with value 200
Inserted key 2 with value 400
Inserted key 3 with value 600
Inserted key 4 with value 800
Inserted key 5 with value 1000
Inserted key 6 with value 1200
Inserted key 7 with value 1400
Inserted key 8 with value 1600
Inserted key 9 with value 1800
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358
Key ed75a354 : Value pointer ed75a358

这是我尝试打印地址内容后发生的情况

printf("Key %i : Value pointer %i\n", key, value);
Inserted key 1 with value 200
Inserted key 2 with value 400
Inserted key 3 with value 600
Inserted key 4 with value 800
Inserted key 5 with value 1000
Inserted key 6 with value 1200
Inserted key 7 with value 1400
Inserted key 8 with value 1600
Inserted key 9 with value 1800
Segmentation fault (core dumped)

哈希表的实现代码

HashTable *hash_table_new(HashTableHashFunc hash_func,
                          HashTableEqualFunc equal_func)
{
    HashTable *hash_table;

    /* Allocate a new hash table structure */

    hash_table = (HashTable *) malloc(sizeof(HashTable));

    if (hash_table == NULL) {
        return NULL;
    }

    hash_table->hash_func = hash_func;
    hash_table->equal_func = equal_func;
    hash_table->key_free_func = NULL;
    hash_table->value_free_func = NULL;
    hash_table->entries = 0;
    hash_table->prime_index = 0;

    /* Allocate the table */

    if (!hash_table_allocate_table(hash_table)) {
        free(hash_table);

        return NULL;
    }

    return hash_table;
}

int hash_table_insert(HashTable *hash_table, HashTableKey key,
                      HashTableValue value)
{
    HashTableEntry *rover;
    HashTablePair *pair;
    HashTableEntry *newentry;
    unsigned int index;

    /* If there are too many items in the table with respect to the table
     * size, the number of hash collisions increases and performance
     * decreases. Enlarge the table size to prevent this happening */

    if ((hash_table->entries * 3) / hash_table->table_size > 0) {

        /* Table is more than 1/3 full */

        if (!hash_table_enlarge(hash_table)) {

            /* Failed to enlarge the table */

            return 0;
        }
    }

    /* Generate the hash of the key and hence the index into the table */

    index = hash_table->hash_func(key) % hash_table->table_size;

    /* Traverse the chain at this location and look for an existing
     * entry with the same key */

    rover = hash_table->table[index];

    while (rover != NULL) {

        /* Fetch rover's HashTablePair entry */

        pair = &(rover->pair);

        if (hash_table->equal_func(pair->key, key) != 0) {

            /* Same key: overwrite this entry with new data */

            /* If there is a value free function, free the old data
             * before adding in the new data */

            if (hash_table->value_free_func != NULL) {
                hash_table->value_free_func(pair->value);
            }

            /* Same with the key: use the new key value and free
             * the old one */

            if (hash_table->key_free_func != NULL) {
                hash_table->key_free_func(pair->key);
            }

            pair->key = key;
            pair->value = value;

            /* Finished */

            return 1;
        }

        rover = rover->next;
    }

    /* Not in the hash table yet.  Create a new entry */

    newentry = (HashTableEntry *) malloc(sizeof(HashTableEntry));

    if (newentry == NULL) {
        return 0;
    }

    newentry->pair.key = key;
    newentry->pair.value = value;

    /* Link into the list */

    newentry->next = hash_table->table[index];
    hash_table->table[index] = newentry;

    /* Maintain the count of the number of entries */

    ++hash_table->entries;

    /* Added successfully */

    return 1;
}

HashTableValue hash_table_lookup(HashTable *hash_table, HashTableKey key)
{
    HashTableEntry *rover;
    HashTablePair *pair;
    unsigned int index;

    /* Generate the hash of the key and hence the index into the table */

    index = hash_table->hash_func(key) % hash_table->table_size;

    /* Walk the chain at this index until the corresponding entry is
     * found */

    rover = hash_table->table[index];

    while (rover != NULL) {
        pair = &(rover->pair);

        if (hash_table->equal_func(key, pair->key) != 0) {

            /* Found the entry.  Return the data. */

            return pair->value;
        }

        rover = rover->next;
    }

    /* Not found */

    return HASH_TABLE_NULL;
}

1 个答案:

答案 0 :(得分:1)

您的指针有些混乱。 如果您替换:

for (int i = 0; ...

使用

int i;
for (i = 0; ...

用于main中的第一个循环,并带有:

对于(i = 0; ...

在第二个循环中,您将获得期望的输出。

如果将此行添加到equalFunc中:

fprintf(stderr,"cmp %p/%d, %p/%d\n", key1, *key1, key2, *key2);

您会注意到equalFunc的两个参数始终相同-实际上它们指向您的本地“ i”变量。 因此,这不适用于两个单独的“ i”值。

使用调试器进行调试会显示:

void *v_value = hash_table_lookup(mapMatrices, &i);

在您的源代码中返回NULL [即未找到],但是您还是继续取消引用它。

要使您的程序正确,您可能应该按值而不是引用传递键:

int stat = hash_table_insert(mapMatrices, (int *)i, (int *)value);
...
void *v_value = hash_table_lookup(mapMatrices, (int *)i);
int value = (int )v_value;
...
/* equalFunc: */
return (int)value1 == (int)value2;

但是出于卫生原因,请使用intptr_t和无关的演员,减少行为不明的邪恶之神烧毁您的房屋。