gpt4 book ai didi

C 段错误核心转储

转载 作者:行者123 更新时间:2023-11-30 20:51:58 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的链表程序,但遇到了这个问题。我相信“段错误和核心转储”问题与内存有关,但有人可以解释一下我将如何解决这个问题。

编辑:在list_mgr.c的主函数中的第一个打印语句之后发生错误

这是类(class)代码,我必须源文件(list_mgr.c,list_funcs.c)和头文件(list_funcs.h)这是list_mgr.c

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list_funcs.h"
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
int main(void) {
struct data_node *first, *new_node, *ptr;
printf("Insert first node into list\n");
first=ptr=insert(&first, 5);
strcpy(ptr->name,"Alpha");
ptr=insert(&first, 7);
strcpy(ptr->name,"Beta");
ptr=insert(&first, 3);
strcpy(ptr->name,"Charlie");
dump_list(first);

printf("Search found: ");
ptr=find_node(first, 5);
dump_node(ptr);
printf("Deleting non-1st node.\n");
delete(&first, 7);
dump_list(first);



return 0;

}

这里是list_funcs.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list_funcs.h"
struct data_node * insert (struct data_node **p_first, int elem) {


struct data_node *new_node, *prev, *current;
current=*p_first;
while (current != NULL && elem > current->data) {
prev=current;
current=current->next;
} /* end while */
/* current now points to position *before* which we need to insert */
new_node = (struct data_node *) malloc(sizeof(struct data_node));
new_node->data=elem;

new_node->next=current;
if ( current == *p_first ) /* insert before 1st element */
*p_first=new_node;
else /* now insert before current */
prev->next=new_node;
/* end if current == *p_first */
return new_node;
};

struct data_node * find_node (struct data_node *p, int elem) {
while (p != NULL) {
if ( elem == p->data )
return p;
p=p->next;
} /* end while */
}; /* end find_node */

void dump_node (struct data_node *current) {
printf("Dumping node: ");
if (current != NULL)
printf("%s: %d\n", current->name, current->data);
}; /* end dump_list */


void dump_list (struct data_node *current) {
printf("List dump:\n");
while (current != NULL) {
printf("%s: %d\n", current->name, current->data);
current=current->next;
} /* end while */
printf("\n");
}; /* end dump_list */

int delete (struct data_node **p_first, int elem) {
int retval = 0;
struct data_node *current, *prev;
current=*p_first;
while (current != NULL && elem != current->data ) {
prev=current;
current=current->next;
}
if (current == NULL) /* element not found */
return retval;
/* current now points to node to delete */
if ( current == *p_first ) /* delete 1st node */
*p_first = (*p_first)->next;
else /* link previous to next thus skipping over node to delete */
prev->next=current->next;
free(current);
retval=1;
return retval;
}; /* end delete */

这是头文件

 #define STRINGMAX 25

struct data_node {
char name [STRINGMAX];
int data;
struct data_node *next;
};



struct data_node * insert (struct data_node **, int);
struct data_node * find_node (struct data_node *, int);
void dump_list (struct data_node *);
int delete (struct data_node **, int);
void dump_node (struct data_node *);

最佳答案

您访问未初始化的变量:

int main(void) {
struct data_node *first, *new_node, *ptr;
first=ptr=insert(&first, 5);
//...
struct data_node * insert (struct data_node **p_first, int elem) {
struct data_node *new_node, *prev, *current;
current=*p_first;

*p_firstmain 中的 first,它从未初始化。因此 current 获得一个野值,然后您可以取消引用该值。

编辑:我还没有仔细阅读您的所有代码来尝试理解它,但也许这个修复会起作用:

int main(void) {
struct data_node *first = NULL, *new_node, *ptr;
ptr=insert(&first, 5);
first = ptr;

我将这两个赋值分开,因为有时 a = foo(&a) 是未定义的。

关于C 段错误核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com