我正在尝试编写一个函数,该函数将释放链表的所有节点(存储桶)。当我尝试运行以下代码时,尽管我不确定为什么我认为自己可以正确释放,但是却收到malloc错误。任何见解将不胜感激。
typedef struct bucket bucket;
struct bucket {
char *string;
unsigned long int hash;
bucket *next;
};
void bucket_free(bucket *b)
{
if(b == NULL){
free(b);
}
else{
free(b->string);
bucket_free(b->next);
free(b);
}
}
运行此代码时,我得到:
a.out(58188,0x7fff943f7380) malloc: *** error for object 0x106ec4fa6: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6