0

我试图用 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=='\0') || i == 29)
            {
                if(i==0)
                {
                    continue;
                }
                tempStr[i]='\0';
                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=='\0') || i == 29)
            {
                if(i==0)
                {
                    continue;
                }
                tempStr[i]='\0';
                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;
}

但我仍然不知道我的代码出了什么问题。谁能帮我理解,这样我以后就不会重复我的错误了?

4

4 回答 4

4

据我所知,这两个版本是相同的,但错误很容易分辨。这是带有一些注释的代码

// 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;,您会更接近。

于 2013-08-28T07:00:48.927 回答
3

您的“临时”为 NULL:

curr=NULL;

然后:

struct list *temp = curr;

最后:

temp->next = curr;

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

我知道这对你没有多大帮助,但我发现代码还有其他几个问题,而且不容易阅读。

既然您标记了这个 C++,您是否考虑过使用其中一个 std 容器?像 std::list 一样?

于 2013-08-28T07:01:04.717 回答
3

在 while 循环之前,您将 NULL 分配给 curr

curr=NULL;

然后,您将 curr 分配给 temp

struct list *temp = curr;

然后当你这样做

temp->next = curr;

你会得到一个分段错误,因为 NULL 没有下一个指针。

如果你删除curr=NULL;,你应该没问题。

于 2013-08-28T07:01:26.873 回答
0

问题是你做的

curr=NULL;

就在他的while循环之前

于 2013-09-13T14:01:24.003 回答