简体   繁体   English

设置struct value = segfault

[英]Setting struct value = segfault

#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";

this is where I seem to be having the problem. 这是我似乎遇到问题的地方。

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

I'm trying to create a hash table, but whenever I try to set a value in the toInsert struct pointer it segfaults. 我正在尝试创建一个哈希表,但每当我尝试在toInsert结构指针中设置一个值时,就会出现段错误。 Can someone explain to me what I am doing wrong? 有人可以向我解释我做错了什么吗?

Try this: 尝试这个:

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

Also, I can't spot where you reserve memory for your hash_table. 另外,我无法找到为hash_table保留内存的位置。 Is it hidden somewhere else?? 它隐藏在其他地方吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM