【问题标题】:Setting struct value = segfault设置结构值 = 段错误
【发布时间】:2012-03-22 01:39:18
【问题描述】:
#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 结构指针中设置一个值时,它都会出现段错误。有人可以向我解释我做错了什么吗?

【问题讨论】:

  • k = "sayhello"; 应该是strcpy(k, "sayhello");
  • char *k = (char*)malloc(sizeof(char*)); 没有保留足够的空间来存储"sayhello"(即malloc(strlen("sayhello")+1))。
  • 我尝试了这两个,但没有运气。仍然出现段错误。
  • 你使用table而不初始化它。

标签: c segmentation-fault hashtable


【解决方案1】:

试试这个:

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 保留内存的位置。是不是藏在别的地方??

【讨论】:

  • @AaronThomasBlakely 那么你也应该为你的哈希表分配内存。
  • 可能是这样,我忘了给它预留内存。
  • 我正在尝试使用:struct hash_table *table; table = (struct hash_table *)malloc(sizeof(*table));但它开始抛出关于冲突类型的错误。
  • 冲突类型?如果是关于strdup,那么你只需要包含string.h。我让它在这里运行,但它会引发 浮点异常
  • pastebin.com/MgNEEtgP 我得到了那些,当我尝试在桌子上使用 malloc() 时,但如果我不使用 malloc() 它编译得很好。
猜你喜欢
  • 1970-01-01
  • 2021-06-12
  • 2018-12-25
  • 2011-08-24
  • 2020-02-17
  • 2014-01-18
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多