程序接收信号SIGSEGV,分段故障,链表程序 #包括 #包括 类型定义结构节点{ int值; 结构节点*下一步; }节点; 节点*createNode(int数据); 节点*插入前端(节点*第一,节点*新节点); 作废打印列表(节点*第一); 作废删除列表(节点*第一); int main(int argc,常量字符**argv) { int numItems,ch; 文件*fp; fp=fopen(argv[1],“r”); 而((ch=getc(fp))!=EOF) { if(ch='\n')numItems++; } fclose(fp); Node*first=NULL; 节点*新节点; 节点*下一步; int i; 对于(i=1;i值=数据; newNode->next=NULL; 返回newNode; } 节点*insertFront(节点*first,节点*newNode) { if(newNode==NULL){ /*把手*/ } newNode->next=NULL; if(first==NULL){ 第一个=新节点; } 否则{ 节点*温度=第一; while(临时->下一步!=NULL) { 温度=温度->下一步; } temp->next=newNode; 第一个=新节点; } 先返回; } 无效打印列表(节点*第一个) { 节点*温度; 温度=第一; printf(“链表中的元素是\n”); while(temp!=NULL) { printf(“%d\n”,temp->value); 温度=温度->下一步; } } 无效删除列表(节点*第一个) { 节点*温度; 温度=第一; 第一个=第一个->下一个; temp->next=NULL; 免费(临时); }
我试着用gdb运行,这就是我得到的,第一次尝试制作链接列表的真实体验 程序接收信号SIGSEGV,分段故障。 _IO_getc(fp=0x0)在getc.c:40处 40个IO获取锁(fp)程序接收信号SIGSEGV,分段故障,链表程序 #包括 #包括 类型定义结构节点{ int值; 结构节点*下一步; }节点; 节点*createNode(int数据); 节点*插入前端(节点*第一,节点*新节点); 作废打印列表(节点*第一); 作废删除列表(节点*第一); int main(int argc,常量字符**argv) { int numItems,ch; 文件*fp; fp=fopen(argv[1],“r”); 而((ch=getc(fp))!=EOF) { if(ch='\n')numItems++; } fclose(fp); Node*first=NULL; 节点*新节点; 节点*下一步; int i; 对于(i=1;i值=数据; newNode->next=NULL; 返回newNode; } 节点*insertFront(节点*first,节点*newNode) { if(newNode==NULL){ /*把手*/ } newNode->next=NULL; if(first==NULL){ 第一个=新节点; } 否则{ 节点*温度=第一; while(临时->下一步!=NULL) { 温度=温度->下一步; } temp->next=newNode; 第一个=新节点; } 先返回; } 无效打印列表(节点*第一个) { 节点*温度; 温度=第一; printf(“链表中的元素是\n”); while(temp!=NULL) { printf(“%d\n”,temp->value); 温度=温度->下一步; } } 无效删除列表(节点*第一个) { 节点*温度; 温度=第一; 第一个=第一个->下一个; temp->next=NULL; 免费(临时); },c,segmentation-fault,C,Segmentation Fault,我试着用gdb运行,这就是我得到的,第一次尝试制作链接列表的真实体验 程序接收信号SIGSEGV,分段故障。 _IO_getc(fp=0x0)在getc.c:40处 40个IO获取锁(fp) 我不确定我在这里做错了什么?感谢您提前提供的任何提示。您没有将numItems初始化为零。它可以是任何数字,包括负数。因此,您的列表没有创建,因此指针首先指向NULL。然后,当函数deleteList尝试释放内存时,代码会在函数deleteList中出错t location NULL。numItems未初始
我不确定我在这里做错了什么?感谢您提前提供的任何提示。您没有将numItems初始化为零。它可以是任何数字,包括负数。因此,您的列表没有创建,因此指针首先指向NULL。然后,当函数deleteList尝试释放内存时,代码会在函数deleteList中出错t location NULL。
numItems
未初始化。看起来它无法打开文件。fopen()返回NULL,将其传递给getc(),然后崩溃。始终检查fopen()的返回值以及其他容易失败的库函数!在提出问题之前,请阅读。您可能可以通过修剪问题来解决此问题。执行free(NULL)是可以的,但读取未初始化的数据是未定义的行为。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node* next;
}Node;
Node* createNode(int data);
Node* insertFront(Node* first, Node* newNode);
void printList(Node* first);
void deleteList(Node* first);
int main(int argc, const char **argv)
{
int numItems, ch;
FILE *fp;
fp = fopen(argv[1], "r");
while ((ch = getc(fp)) != EOF)
{
if (ch = '\n') numItems++;
}
fclose(fp);
Node *first = NULL;
Node *newNode;
Node *Next;
int i;
for(i = 1; i <= numItems; i++)
{
newNode = createNode(i);
first = insertFront(first, newNode);
}
printList(first);
deleteList(first);
return 1;
}
Node* createNode(int data)
{
Node *newNode;
newNode = malloc(sizeof(Node));
newNode -> value = data;
newNode -> next = NULL;
return newNode;
}
Node* insertFront(Node* first, Node* newNode)
{
if (newNode == NULL) {
/* handle oom */
}
newNode->next=NULL;
if (first == NULL) {
first = newNode;
}
else {
Node *temp=first;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next=newNode;
first = newNode;
}
return first;
}
void printList(Node* first)
{
Node *temp;
temp=first;
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d\n",temp->value);
temp=temp->next;
}
}
void deleteList(Node* first)
{
Node *temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}