用 C 语言实现一个带有奇怪段错误的哈希表

问题描述

我正在尝试在 C 中创建一个哈希表,并且我想记录一个包含表的每个唯一键的链表。但是,我在 while 循环的一开始就遇到了 seg 错误,我用它来检查某个键是否已经在链表中。谁能理解这里发生了什么?

提前道歉:ull 是 unsigned long long

h_keys 是一个全局变量

seg 错误发生在这个 while 循环的开始处。

void put_in_key(ull address){
    struct mem_node * cur = h_keys;
    ull key = hash_func(address);
    printf("ok %lld\n",address);
    while(cur){
        //printf("stuck in here?\n");
        if(cur->key==key){ // key already in hashtable keys
            return;
        }
        cur=cur->next;
    }
    struct mem_node* newkey = malloc(sizeof(struct mem_node *));
    if(newkey==NULL){
        printf("big trouble");
    }
    newkey->key=key;
    cur->next=newkey;
}

这是结构

struct mem_node{ // add one of these each time we get a new window
    ull address;
    ull page;
    ull key;
    struct mem_node * next;
};

这里是 h_keys 的声明

h_keys=malloc(sizeof(struct mem_node*));

然后根据输入,我有时会到达计算链表长度的另一个函数的一部分。但是,我在此处的 while 循环的第一行也遇到了段错误

struct mem_node* counter = h_keys;
        ull uniques=0;
        while(counter!=NULL && counter->next!=NULL){
            printf("whafs da %lld\n",uniques);
        
            //printf("%lld\n",counter->key);
            //printf("%lld \n",counter->ne)
            fflush(stdout);
            uniques++;
            counter=counter->next;
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)