C哈希表实现中的分段错误

C哈希表实现中的分段错误,c,segmentation-fault,hashtable,C,Segmentation Fault,Hashtable,我正在做一个练习,其中我必须用C创建一个哈希表实现。到目前为止,我已经设置了基本的数据结构,但是我遇到了一个分段错误,我似乎无法确定。我已经设法找出了问题出在哪里,但除此之外我还不确定 如果这很重要,下面是我用来编译的命令:gcc main.c hash_table.c-o main-w 哈希表。c: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hash_

我正在做一个练习,其中我必须用C创建一个哈希表实现。到目前为止,我已经设置了基本的数据结构,但是我遇到了一个分段错误,我似乎无法确定。我已经设法找出了问题出在哪里,但除此之外我还不确定

如果这很重要,下面是我用来编译的命令:
gcc main.c hash_table.c-o main-w

哈希表。c:

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

#include "hash_table.h"

static ht_item* ht_new_item(const char* k, const char* v){
    ht_item* item = malloc(sizeof(ht_item));
    item->key = strdup(k);
    item->value = strdup(v);
    return item;
}

static void ht_del_item(ht_item* item){
    free(item->key);
    free(item->value);
    free(item);
}

ht_table* ht_new(){
    ht_table* table = malloc(sizeof(ht_table));
    table->size = 53;
    table->count = 0;
    table->items = calloc((size_t) table->size, sizeof(ht_item*));
    return table;
}

void ht_del(ht_table *table){
    for(int k = 0; k < table->size; ++k){
        ht_item* item = table->items[k];
        if(item != NULL) ht_del_item(item);
    }
    free(table->items);
    free(table);
}

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_table;
#include "hash_table.h"

int main(){
    ht_table* table = ht_new();
    ht_del(table);
}
main.c(测试代码):

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

#include "hash_table.h"

static ht_item* ht_new_item(const char* k, const char* v){
    ht_item* item = malloc(sizeof(ht_item));
    item->key = strdup(k);
    item->value = strdup(v);
    return item;
}

static void ht_del_item(ht_item* item){
    free(item->key);
    free(item->value);
    free(item);
}

ht_table* ht_new(){
    ht_table* table = malloc(sizeof(ht_table));
    table->size = 53;
    table->count = 0;
    table->items = calloc((size_t) table->size, sizeof(ht_item*));
    return table;
}

void ht_del(ht_table *table){
    for(int k = 0; k < table->size; ++k){
        ht_item* item = table->items[k];
        if(item != NULL) ht_del_item(item);
    }
    free(table->items);
    free(table);
}

typedef struct {
    char* key;
    char* value;
} ht_item;

typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_table;
#include "hash_table.h"

int main(){
    ht_table* table = ht_new();
    ht_del(table);
}

你试过通过
valgrind
运行它吗?你的
53个
项目都没有分配,但你正在尝试释放所有项目。@CoffeeTableEspresso我有-
valgrind--track origins=yes./main
我得到
==12414==错误摘要:0个上下文中的0个错误(被抑制:0个从0个中删除)
I已更改#“hash_table.h”将“hash_table.c”包含在主.c文件中,它工作时没有任何错误。如果可能的话,您可以显示错误日志。您的编译器应该已经警告过您这一点;建议打开警告。建议
ht_new(void)
()
不是原型。