代码抛出分段错误 我试图帮助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; }
我犯了一个错误 最后,我设法解决了这个问题,从网络上获取了一些代码,而不是我的:代码抛出分段错误 我试图帮助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; },c++,list,C++,List,我犯了一个错误 最后,我设法解决了这个问题,从网络上获取了一些代码,而不是我的: #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() { 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;
}
#包括
#包括
#包括
使用名称空间std;
结构列表*createlist(文件*m);
结构列表
{
字符*数据;
结构列表*下一步;
}名单;
main()
{
chara[100],ch;
结构列表*obj;
cout据我所知,这两个版本是相同的,但错误很容易辨别。这是您的代码和一些注释
// 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”为NULL:
curr=NULL;
之后:
struct list *temp = curr;
最后:
temp->next = curr;
您正在尝试使用具有空值的结构指针
我知道这不会对您有多大帮助,但我发现代码还有其他一些问题,而且不容易阅读
既然你标记了这个C++,你是否认为使用STD容器中的一个?像STD::List.< /P>< P>在while循环之前,你将NULL分配到Curr
curr=NULL;
然后,将curr分配给temp
struct list *temp = curr;
那么当你这么做的时候
temp->next = curr;
由于NULL没有下一个指针,因此会出现分段错误
如果删除curr=NULL;
,则应该没有问题。问题在于您所做的
curr=NULL;
在Hy-while循环
之前,除非你正在学习,否则你应该使用标准库中已经存在的列表。如果你想学习,我建议尝试学习一个类似于标准的通用类。请决定你是否想使用C或C++ C++。你不知道C(你有语法不是C),你也不知道正确的现代C++风格。
curr=NULL;