代码引发分段错误
Code throws segmentation fault
我曾试图用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循环之前
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)