C 在特定索引下释放时出现分段错误

C 在特定索引下释放时出现分段错误,c,free,C,Free,在散列表上遵循这一点,我实现了一种简化版本。但是,每当在每4个索引(4,8,12…)上调用delete\u entry,我就会得到一个分段错误。除此之外,它工作正常 以下是我运行的代码: #include <stdio.h> typedef struct { int key; int value; } entry; typedef struct{ int length; int count; entry** items; } table;

在散列表上遵循这一点,我实现了一种简化版本。但是,每当在每4个索引(4,8,12…)上调用
delete\u entry
,我就会得到一个分段错误。除此之外,它工作正常

以下是我运行的代码:

#include <stdio.h>

typedef struct {
    int key;
    int value;
} entry;

typedef struct{
    int length;
    int count;
    entry** items;
} table;

entry *new_entry(const int, const int);
table *create_table();
void destroy_table(table *t);
void delete_entry(entry *e);
void table_insert(table *, const int, const int);

int main(){
    table *new_table = create_table();
    printf("[INFO] Created new table successfully!!\n");
    
    for(int i=0; i<16; i++){
        table_insert(new_table,i,i+i);
    }
    printf("[INFO] Inserted %d items\n",new_table->count);

    destroy_table(new_table);
    printf("[INFO] Destroyed table successfully!!\n");
    return 0;

}

table *create_table(){
    table *new_table = malloc(sizeof(table));
    if(new_table == NULL){
        printf("[WARNING] Malloc failure, returning NULL");
        return NULL;
    }
    new_table->length = 16;
    new_table->count = 0;
    new_table->items = calloc((size_t)new_table->length, sizeof(entry*));
    
    return new_table;
}

entry *new_entry(const int k, const int val){
    entry *new_item = malloc(sizeof(entry));
    if(new_item == NULL){
        printf("[WARNING] Malloc failure, returning NULL");
        return NULL;
    }
    new_item->key = k;
    new_item->value = val;
    
    return new_item;
}
void destroy_table(table *t){
    for(int i=0; i<t->count; i++){
        if(i== 4 || i == 8|| i==12) //Without this program terminates after 3rd index
            continue;
        entry *e = t->items[i];
        if (e != NULL){
            delete_entry(e);
        }
    }
    free(t->items);
    free(t);
}
void delete_entry(entry *e){
    free(e->key);
    free(e->value);
    free(e);
}

void table_insert(table *t, const int k, const int v){
    entry *e = new_entry(k, v);
    t->items[t->count] = e;
    t->count++;
}
#包括
类型定义结构{
int键;
int值;
}入境;
类型定义结构{
整数长度;
整数计数;
条目**项目;
}表;
条目*新条目(常量int,常量int);
表*创建_表();
空洞破坏_表(表*t);
作废删除_条目(条目*e);
空表_插入(表*,常数int,常数int);
int main(){
table*new_table=创建_table();
printf(“[INFO]已成功创建新表!!\n”);
for(int i=0;i计数);
销毁_表(新_表);
printf(“[INFO]已成功销毁表!!\n”);
返回0;
}
表*创建_表(){
表*new_table=malloc(sizeof(table));
if(新表==NULL){
printf(“[警告]Malloc失败,返回NULL”);
返回NULL;
}
新表格->长度=16;
新建表格->计数=0;
新建表格->项目=calloc((大小)新建表格->长度,大小(条目*);
返回新的_表;
}
条目*新条目(常量整数k,常量整数val){
条目*新项目=malloc(条目大小);
如果(新项目==NULL){
printf(“[警告]Malloc失败,返回NULL”);
返回NULL;
}
新建项目->键=k;
新建项目->值=val;
返回新项目;
}
空表(表*t){
for(int i=0;i计数;i++){
如果(i==4 | | i==8 | | i==12)//没有此程序,则在第三个索引后终止
继续;
录入*e=t->项目[i];
如果(e!=NULL){
删除(e)项;
}
}
免费(t->项目);
自由(t);
}
作废删除条目(条目*e){
免费(e->key);
自由(e->value);
免费(e);
}
空表插入(表*t,常数k,常数v){
条目*e=新条目(k,v);
t->items[t->count]=e;
t->count++;
}

尝试将
删除条目()
替换为:

void delete_entry(entry *e){
    free(e);
}

释放整数
e->key
e->value
的值可能会导致未定义的行为。您应该仅在由
malloc()
和朋友生成的对象上使用
free()

free(e->key)看起来可疑,您试图释放int值,很可能UB@tstanisl在这种情况下,有什么建议可以避免吗?
free(e->value)
也有同样的问题。规则很简单:
free
只能使用
malloc
返回的确切值调用。因此在这种情况下,只有
free(e)
有效。删除它之前的另外两个
free
调用。@kaylum为什么需要在教程中的结构本身之前释放
?教程字段是用
strdup
分配的指针。所以他们需要被释放。我先前的评论只是简单化了<对于由
malloc
calloc
strdup
和所有其他动态分配函数分配的任何动态内存,都可以调用code>free