代码引发分段错误

Code throws segmentation fault

本文关键字:错误 分段 代码      更新时间:2023-10-16

我曾试图用c++帮助一位朋友编写列表代码。我写过:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct list* createlist(FILE *m);
struct list
{
    char *data;
    struct list *next;
}list;
main()
{
    char a[100], ch;
    struct list* obj;
    cout<<"Enter the name of the file for obtaining input.."<<endl;
    cin>>a;
    FILE *in;
    in=fopen(a,"r");
    if(in!=NULL)
    {
        ch=fgetc(in);
        if(ch=='1')
        obj=createlist(in);
        fclose(in);
    }
    return 0;
}
struct list* createlist(FILE *m)
{
    cout<<"Entered createlist function..!"<<endl;
    char *tempStr = (char *)malloc(30 * sizeof(char));
    struct list *curr, *head = (struct list *)malloc(sizeof(struct list));
    curr = head;
    curr->data = tempStr;
    char c;
    int i=0;
    curr=NULL;
     while(EOF!=(c=fgetc(m)))
        {
            if((c==' ') || (c=='') || i == 29)
            {
                if(i==0)
                {
                    continue;
                }
                tempStr[i]='';
                i=0;
                struct list *temp = curr;
                curr = (struct list *)malloc(sizeof(struct list));
                temp->next = curr;
                tempStr = (char *)malloc(30 * sizeof(char));
                curr->data = tempStr;
                continue;
            }
            tempStr[i]=c;
            i++;
        }
    return head;
}

但是代码抛出异常。我试着理解出了什么问题,并更改了2-3个小时的代码,但无法理解。我正在为列表项分配空间,但当我尝试在行为next分配值时

temp->next = curr;

我有分段错误。

最后,我通过从网上而不是我的代码来解决了这个问题:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct list* createlist(FILE *m);
struct list
{
    char *data;
    struct list *next;
}list;
main()
{
    char a[100], ch;
    struct list* obj;
    cout<<"Enter the name of the file for obtaining input.."<<endl;
    cin>>a;
    FILE *in;
    in=fopen(a,"r");
    if(in!=NULL)
    {
        ch=fgetc(in);
        if(ch=='1')
        obj=createlist(in);
        fclose(in);
    }
    return 0;
}
struct list* createlist(FILE *m)
{
    cout<<"Entered createlist function..!"<<endl;
    char *tempStr = (char *)malloc(30 * sizeof(char));
    struct list *curr, *head = (struct list *)malloc(sizeof(struct list));
    curr = head;
    curr->data = tempStr;
    char c;
    int i=0;
    curr=NULL;
    while(EOF!=(c=fgetc(m)))
        {
            if((c==' ') || (c=='') || i == 29)
            {
                if(i==0)
                {
                    continue;
                }
                tempStr[i]='';
                i=0;
                struct list *temp = curr;
                curr = (struct list *)malloc(sizeof(struct list));
                temp->next = curr;
                tempStr = (char *)malloc(30 * sizeof(char));
                curr->data = tempStr;
                continue;
            }
            tempStr[i]=c;
            i++;
        }
    return head;
}

但我仍然不知道我的代码出了什么问题。有人能帮我理解吗?这样我以后就不会再犯错误了?

据我所见,这两个版本是相同的,但错误很容易判断。这是你的代码和一些评论

// at this point curr is NULL (see start of while loop)
struct list *temp = curr;
// so now temp is NULL
curr = (struct list *)malloc(sizeof(struct list));
// now curr is pointing at some memory, but temp is still NULL
temp->next = curr;
// temp is NULL so this crashes

和其他人一样,我认为如果你去掉curr = NULL;,你会更接近。

您的"temp"为空:

curr=NULL;

之后:

struct list *temp = curr;

最后:

temp->next = curr;

您正试图使用具有NULL值的结构指针。

我知道这不会对你有多大帮助,但我看到了代码中的其他几个问题,而且不容易阅读。

既然你标记了这个C++,你有没有考虑过使用其中一个std容器?比如std::list?

在while循环之前,您正在将NULL赋值给curr

curr=NULL;

然后,您将当前分配给临时

struct list *temp = curr;

然后当你做

temp->next = curr;

由于NULL没有下一个指针,因此会出现分段错误。

如果您删除curr=NULL;,您应该没事。

问题是你做

curr=NULL;

就在while循环之前