C 双链表故障(核心转储)

C 双链表故障(核心转储),c,doubly-linked-list,C,Doubly Linked List,最近我一直在练习数据结构,但遇到了一个新问题,当我尝试打印列表时,它会提示“core dumped”,我很困惑 我宣布: struct DOUBLE_LINK_LIST { char *string; struct DOUBLE_LINK_LIST *pre; struct DOUBLE_LINK_LIST *next; }double_link_list; 链接列表.h: /* double linked list */ struct DOUBLE_LINK_LIST *init

最近我一直在练习数据结构,但遇到了一个新问题,当我尝试打印列表时,它会提示“core dumped”,我很困惑

我宣布:

struct DOUBLE_LINK_LIST {
  char *string;
  struct DOUBLE_LINK_LIST *pre;
  struct DOUBLE_LINK_LIST *next;
}double_link_list;
链接列表.h:

/* double linked list */
struct DOUBLE_LINK_LIST *init_double_link_list() {
   struct DOUBLE_LINK_LIST *new_link = malloc(sizeof(struct DOUBLE_LINK_LIST));
   if (new_link==NULL) {
      fprintf(stderr, "Insufficient memory!!!");
      return NULL;
   }
   new_link->string = NULL;

   return new_link;
}
============================== 以下是我获得长度的方法:

int D_get_length(struct DOUBLE_LINK_LIST *link) {
  int count;
  count = 0;

  while (link->next!=NULL) {
    count += 1;
    link = link->next;
  }

  return count;
}

int D_create(struct DOUBLE_LINK_LIST *link, char *in) {
if (D_get_length(link)>=STRING_SIZE) {
    fprintf(stderr, "Double link list is full!!!");
    return ERROR;
}
else {
    struct DOUBLE_LINK_LIST *new_node;
    new_node = init_double_link_list();
    if (new_node==NULL) {
        return ERROR;
    }
    /*new_node->next = link->next;
    new_node->pre = link;
    link->next = new_node;
    if(new_node->next!=NULL) {
        new_node->next->pre = new_node;
    }
    new_node->string = in;*/
    new_node->next = link->next;
    link->next = new_node;
    new_node->string = in;
}

return OK;
}
它确实返回正确的长度,但在create函数中不返回:

char *D_get_element(struct DOUBLE_LINK_LIST *link, int pos) {
int i;

if (D_is_empty(link)==YES) {
    return NULL;
}

printf("_%d_", D_get_length(link));

if (pos <= 0 || pos-1 > D_get_length(link)) {
    fprintf(stderr, "Invalid position!!!");
    exit(1);
}
else {
    for (i = 0; i < pos; ++i) {
        link = link->next;
    }
}

return link->string;
}
char*D\u get\u元素(struct DOUBLE\u LINK\u LIST*LINK,int pos){
int i;
如果(D_为空(链接)=是){
返回NULL;
}
printf(“%d”,d_获取长度(链接));
如果(位置D_获取长度(链接)){
fprintf(stderr,“无效位置!!!”);
出口(1);
}
否则{
对于(i=0;i下一步;
}
}
返回链接->字符串;
}

使用调试信息编译并使用Valgrind

问题在于创建的第一个
双链接列表
,我假设您也使用
初始化双链接列表
创建第一个。因为您只执行了
malloc
,而没有清除分配的内存,所以
next
指针上有一些垃圾

然后我假设您开始将字符串推入其中,因为您的
D_create
将坏的
next
移动到新的
中,这个坏指针被推到列表的末尾

然后,在某个时刻,你们将去到未经授权的土地,并严重死亡,或者,如果你们运气不好,它将工作一段时间,然后再次严重死亡

一种解决方案:使用
calloc
,它将用0初始化内存

其他解决方案:
memset
after
malloc


更多解决方案:在null旁边显式设置

您能告诉我们如何使用这些函数吗?例如,程序崩溃。突出的一点是,我看不到下一个链接的实际初始化位置(例如,
init\u double\u link\u list
没有将它们设置为
NULL
),但是我当然不知道它是否在显示的代码之外完成。请展示一个独立的最小示例,它仍然会转储内核-这意味着显示相同错误的最小
main
。另外,作为参考,如果你有一个核心文件,这是学习如何使用调试器的好时机。你确定“link”总是有效的,所以不为NULL吗?您经常检查link->next,但是第一个元素是什么?@JoachimPileborg我在main中调用这些函数:
struct DOUBLE\u link\u LIST*test4;test4=init_double_link_list();D_create(test4,“万岁”);D_创建(test4,“La”);D_create(test4,“Vida”);printf(“#长度:%d\n”,d#get#u长度(test4));/*for(i=1;我有一个很好的提示,但没有真正回答“问题”。什么是“问题”?我已经在
init_double_link_list
中将next设置为
null
,但问题仍然存在。我在我的单链接列表上使用了相同的方法,并且成功了,这可能是我犯了一些愚蠢的错误,但我检查了很多次…建议逐步调试,只是读取不完整的代码,但问题解释不清楚,请参阅ms还不够。不管怎样,空问题是真的。哦,这真的是我愚蠢的错误造成的问题,我把
==
误输入为
=
。。无论如何,我会更加注意我的代码和内存泄漏,谢谢大家!