【问题标题】:C segmentation fault core dumpedC 分段错误核心转储
【发布时间】:2014-04-07 22:06:37
【问题描述】:

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

编辑:在 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 *);

【问题讨论】:

  • 这么多代码,甚至没有提到导致错误的行。
  • 编辑回答这个问题,错误发生在 list_mgr.c 的主函数中的第一个打印语句之后
  • Seg-Fault 与内存有关!这是来自操作系统的错误,即某些进程试图对没有读取或写入权限的内存块执行某些操作...此错误也会杀死进程,因为这样的非法操作(试图更改一些内存块超出您的界限)是偷偷摸摸的(如果不是随机编程错误),并且表明尝试这样做的进程没有任何好处(实际上它可能会破坏其他进程的运行时)并且必须死掉。所以操作系统会杀死进程(你的程序)......

标签: c list linked-list segmentation-fault


【解决方案1】:

你访问了一个未初始化的变量:

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 是来自 main 的 first,它从未被初始化。所以current 会得到一个狂野的值,然后你可以取消引用。

编辑:我还没有通过你所有的代码来尝试理解它,但也许这个修复会起作用:

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

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

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2017-02-25
    • 2016-07-12
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多