微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决用 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 举报,一经查实,本站将立刻删除。