从更新链表中读取时的Segfault

时间:2017-07-13 01:31:51

标签: c pointers struct hashtable

所以我正在研究哈希表的实现。我对C和指针并不是很有经验而且有点卡住了。

我的哈希表定义如下:

typedef struct KVnode {
  int kv[2];
  struct KVnode *next;
} KVnode;

typedef struct hashtable {
  int size;     // size of hash table
  int entries;  // number of slots allocated in table
  KVnode *table;   /* pointer to table. Each entry will point to linked list
                      of key-value nodes */
} hashtable;

哈希表结构包含KVnode指针表。 KVnode本质上是用于存储冲突的链表。

我的put实现如下:

void put(hashtable* ht, keyType key, valType value){
  int index = hash_key(key, ht->size);

  KVnode *new_node = malloc( sizeof(KVnode) );
  new_node->kv[0] = key;
  new_node->kv[1] = value;
  new_node->next = NULL;

  printf("Inserting at index: %i, key:%i, val:%i \n", index, key, value);

  // If next val is 0, we can set the first node to key-value
  if( ht->table[index].next == 0)
    ht->table[index] = *new_node;
  else{ // find last node in linked list and append new_node
      KVnode cn = ht->table[index];
      while( cn.next != NULL )
        cn = *cn.next;
      cn.next = new_node;
  }
}

这里是整体代码:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 503

typedef struct KVnode {
  int kv[2];
  struct KVnode *next;
} KVnode;

typedef struct hashtable {
  int size;     // size of hash table
  int entries;  // number of slots allocated in table
  KVnode *table;   /* pointer to table. Each entry will point to linked list
                      of key-value nodes */
} hashtable;

typedef int keyType;
typedef int valType;

void init(hashtable**);
int hash_key(keyType key, int size);
void put(hashtable* ht, keyType key, valType value);

void init(hashtable** ht) {
    *ht = (hashtable *) malloc( sizeof(hashtable) );
    if(*ht == NULL){
      printf( "Error: Unable to allocate memory for hashtable" );
      exit(1);
    }
    else{
      (*ht)->entries = 0;
      (*ht)->size = SIZE;
      (*ht)->table = calloc((*ht)->size , sizeof(KVnode *));
    }
}

int hash_key(keyType key, int size){
    return key % size;
}

void put(hashtable* ht, keyType key, valType value){
  int index = hash_key(key, ht->size);

  KVnode *new_node = malloc( sizeof(KVnode) );
  new_node->kv[0] = key;
  new_node->kv[1] = value;
  new_node->next = NULL;

  printf("Inserting at index: %i, key:%i, val:%i \n", index, key, value);

  // If next val is 0, we can set the first node to key-value
  if( ht->table[index].next == 0)
    ht->table[index] = *new_node;
  else{ // find last node in linked list and append new_node
      KVnode cn = ht->table[index];
      while( cn.next != NULL )
        cn = *cn.next;
      cn.next = new_node;
  }
}

int main(){

  hashtable *t = NULL;
  init(&t);

  put(t, 225, 100);
  put(t, 55555, 100);

  printf("node at 255, k:%i, v:%i\n", t->table[225].kv[0],t->table[225].kv[1] );
  printf("node at 255, 2nd node, k:%i, v:%i\n",
         t->table[225].next->kv[0],t->table[225].next->kv[1] );


  free(t);
}

程序编译很好,但在运行时我得到segfault。这是输出:

ds-MacBook-Pro:project0 d$ ./ll
Inserting at index: 225, key:225, val:100
Inserting at index: 225, key:55555, val:100
node at 255, k:55555, v:100
Segmentation fault: 11

我无法弄清楚问题是我的打印声明,还是我实际上没有正确附加链接列表。

0 个答案:

没有答案