我正在尝试创建一个简单的链表程序,但是遇到了这个问题。我相信“分段错误和核心转储”问题与内存有关,但有人可以解释一下我将如何解决此问题。
编辑:在list_mgr.c中的主函数中的第一个打印语句后发生错误
这是课程代码,我必须获取源文件(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_first
是从未初始化的first
中的main
。因此,current
获得一个野值,然后您可以取消引用。编辑:我并没有花很多时间去尝试和理解它,但是也许这个修复会起作用:
int main(void) {
struct data_node *first = NULL, *new_node, *ptr;
ptr=insert(&first, 5);
first = ptr;
我将这两个分配分开,因为有时未定义具有
a = foo(&a)
。关于c - C分割故障核心转储,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22924008/