簡體   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";

這是我似乎遇到問題的地方。

        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結構指針中設置一個值時,就會出現段錯誤。 有人可以向我解釋我做錯了什么嗎?

嘗試這個:

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保留內存的位置。 它隱藏在其他地方嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM