加载文件时出现分段错误(核心转储) [英] segmentation fault (core dump) while loading a file

查看:62
本文介绍了加载文件时出现分段错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个从.txt文件加载数据的函数,但是当它运行时,我总是会遇到分段错误(核心转储)错误.该文件包含未知数量的行,而每行都有一个字符串和一个由tab分隔的整数.list_create函数仅创建数据结构.最后的while循环删除了数据结构,我没有包含代码,因为我确定它不会引起问题,但是我也想表明我正在释放数据结构.值得一提的是,何时使用gdb,我得到:

I am trying to create a function which loads data from a .txt file but when it runs I always get a segmentation fault(core dumped) error. The file contains an unknown number of lines while each line has a string and an integer separated by tab.The list_create function just creates a data structure. The while loop in the end deletes the data structure, I did not include the code because I am sure it does not cause the problem but I also want show that I am freeing the data structure.It is worth mentioning that when is gdb used, I get:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555554c46 in load (filename=0x7fffffffe2ab "students.txt", 
    l=0x555555757260) at Student.c:92
92                  tmp->next=malloc(sizeof(struct _node));

我试图用其他方法来改变feof,在有无错误的情况下使用,并将fopen的模式更改为r而不是a.

I have tried to change the feof with something else,use with and without ferror and change the mode for fopen to r instead of a.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAXSTRING 50
typedef struct{
    char name[MAXSTRING];
    int id;
} student;
typedef struct _node* node;
typedef struct _list* list;

struct _node {
    student data;
    node next;
};

struct _list {
    node head;
    int size;
};


list list_create(){
    list l=(list) malloc(sizeof(struct _list));
    assert(1);
    l->head=NULL;
    l->size=0;
    return l;
}
void load(char*filename,list l){
    FILE *fd=fopen(filename,"r");
    node tmp=l->head;

    if(fd==NULL){
        printf("Error trying to open the file\n");
                abort();
    }
    else{

                while(!feof(fd)&&!ferror(fd)){

        fscanf(fd,"%s\t%d\n",tmp->data.name,&tmp->data.id);   
                tmp->next=(node)malloc(sizeof(struct _node));
        assert(tmp->next);
        tmp=tmp->next;                
        l->size++;
            if (tmp==NULL){
                printf("Error trying to allocate memory\n");
                abort();
            }
                }      
        }

    tmp->next=NULL;
    fclose(fd);
}   



int main(int argc,char *argv[]){ 
list l=list_create();
if(argc!=2){
        printf("Input Error\n");
    }
    load(argv[1],l);
\*Some code*\


while (!list_empty(l)){
                list_freenode(list_deletefirst(l));
        }

    free(l);
        return 0;

我希望能够成功加载文件,能够编辑其组件并保存它们.

I am expecting to load the file successfully, be able to edit its components and save them.

推荐答案

list_create 中,您做

l->head=NULL;

然后在 load 中执行

node tmp=l->head;

所以现在 tmp 为NULL.

但是稍后在 load 中,您将取消引用 tmp (例如,此处 tmp-> next = .... ).因此,您取消引用NULL指针会导致程序崩溃.

But later in load you dereference tmp (e.g. here tmp->next= ....). So you dereference a NULL pointer which causes your program to crash.

这篇关于加载文件时出现分段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆