设置struct value = segfault

时间:2012-03-04 03:52:11

标签: c segmentation-fault hashtable

#include <stdio.h>
#include <stdlib.h>
#define true 1

struct hashRow {
    char *key;
    char *value;
};

struct hash_table {
    int max;
    int number_of_elements;
    struct hashRow **elements;
};

int hashstring(const char *s)
{
    int key = 0;
    while (*s)
        key = key * 37 + *s++;

    return key;
 }

 int hash_fun(int key, int try, int max) {
     return (key + try) % max;
 }

 struct hash_table *table;

 int hash_insert(struct hashRow *data, struct hash_table *hash_table) {
    int try, hash;
    if(hash_table->number_of_elements < hash_table->max) {
        return 0; // FULL
    }
    for(try = 0; true; try++) {
    int hkey = hashstring(data->key);
        hash = hash_fun(hkey, try, hash_table->max);
        if(hash_table->elements[hash] == 0) { // empty cell
            hash_table->elements[hash] = data;
            hash_table->number_of_elements++;
            return 1;
        }
    }
    return 0;
}

struct hashRow *hash_retrieve(char *key, struct hash_table *hash_table) {
    int try, hash;
    for(try = 0; true; try++) {
    int hkey = hashstring(key);
        hash = hash_fun(hkey, try, hash_table->max);
        if(hash_table->elements[hash] == 0) {
            return 0; // Nothing found
        }
        if(hash_table->elements[hash]->key == key) {
            return hash_table->elements[hash];
        }
    }
    return 0;
}


int hash_delete(char *key, struct hash_table *hash_table) {
    int try, hash;
    for(try = 0; true; try++) {
    int hkey = hashstring(key);
        hash = hash_fun(hkey, try, hash_table->max);
        if(hash_table->elements[hash] == 0) {
            return 0; // Nothing found
        }
        if(hash_table->elements[hash]->key == key) {
            hash_table->number_of_elements--;
            hash_table->elements[hash] = 0;
            return 1; // Success
        }
    }
    return 0;
}

void insertsomething()
{

        struct hashRow *toInsert;
    toInsert = (struct hashRow *)malloc(sizeof(*toInsert));

    printf("toInsert declared\n");

    char *k = (char*)malloc(sizeof(char*));
    char *v = (char*)malloc(sizeof(char*));

    k = "sayhello";
    v = "hello";

这就是我似乎遇到问题的地方。

        toInsert->key = k;
        toInsert->value = v;

        hash_insert(toInsert, table);
}

int main()
{
        printf("calling insertsomething.\n");
    insertsomething();
    struct hashRow *gotten;
    gotten = hash_retrieve("sayhello", table);
    printf("test: %s\n", gotten->value);
}

我正在尝试创建一个哈希表,但每当我尝试在toInsert结构指针中设置一个值时,就会出现段错误。有人可以向我解释我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

void insertsomething()
{
    struct hashRow *toInsert;
    toInsert = (struct hashRow *)malloc(sizeof(*toInsert));

    printf("toInsert declared\n");

/*
    char *k = (char*)malloc(sizeof(char*)); // wrong size
    char *v = (char*)malloc(sizeof(char*)); // wrong size

    k = "sayhello"; // bad assignment
    v = "hello";    // bad assignment
*/

    toInsert->key = strdup("sayhello");
    toInsert->value = strdup("hello");

    hash_insert(toInsert, table);
}

另外,我无法找到为hash_table保留内存的位置。它隐藏在其他地方吗?