我的程序使用基于指针的链表。它打开一个文本文件,添加(a)/删除(d)指定链接列表的内容。
该程序在Netbeans(MAC OS X)上运行。但是当我在Linux(RHEL 6.5)上运行程序时,我遇到了分段错误。
我跑了gdb
并在下面发布了错误。任何帮助将不胜感激。
CODE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
char name[42];
struct Node* prev;
struct Node* next;
};
struct List
{
struct Node* head;
struct Node* tail;
};
struct Node* Allocate_node()
{
struct Node *temp = malloc(sizeof (struct Node));
if (temp == NULL)
{
printf("Error: Memory Could Not Be Allocated");
exit(EXIT_FAILURE);
}
temp->prev = NULL;
temp->next = NULL;
return temp;
} /* Allocate_node */
void Free_node(struct Node* node)
{
free(node);
} /* Free_node */
void Free_list(struct List* list)
{
struct Node* curr = NULL;
struct Node* following = NULL;
curr = list->head;
while (curr != NULL)
{
following = curr->next;
Free_node(curr);
curr = following;
}
list->head = list->tail = NULL;
} /* Free_list */
void Insert(struct List *list, char *string)
{
struct Node* curr = list->head;
struct Node* temp = NULL;
while (curr != NULL)
{
if (strcmp(string, curr->name) < 0)
{
break; /* string alphabetically precedes node */
}
else
{
curr = curr->next;
}
}
temp = Allocate_node();
strcpy(temp->name, string);
if (list->head == NULL)
{
list->head = list->tail = temp;
}
else if (curr == NULL)
{
temp->prev = list->tail; // Pointing Tail before New Element
list->tail->next = temp; // Pointing old tail to new tail node
list->tail = temp; // Assigning node to tail
}
else if (curr == list->head)
{
temp->next = list->head;
list->head->prev = temp;
list->head = temp;
}
else
{
temp->next = curr;
temp->prev = curr->prev;
curr->prev = temp;
temp->prev->next = temp;
}
} /* Insert */
void Delete(struct List *list, char *string)
{
struct Node* curr = list->head;
/* Find string */
while (curr != NULL)
{
if (strcmp(string, curr->name) == 0)
{
break;
}
else if (strcmp(string, curr->name) < 0)
{
printf("%s is not in the list\n", string);
return;
}
else
{
curr = curr->next;
}
}
if (curr == NULL)
{
printf("%s is not in the list\n", string);
}
else
{
if (curr->prev == NULL && curr->next == NULL)
{
list->head = list->tail = NULL;
}
else if (curr->prev == NULL)
{
list->head = curr->next;
list->head->prev = NULL;
}
else if (curr->next == NULL)
{
list->tail = curr->prev;
list->tail->next = NULL;
}
else
{
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
}
Free_node(curr);
}
} /* Delete */
void printForward(struct List *list)
{
struct List *temp = list;
while (temp->head != NULL)
{
printf("%s\n", temp->head->name);
temp->head = temp->head->next;
}
}
void printReverse(struct List *list)
{
struct List *temp = list;
while (temp->head != NULL)
{
temp->head = temp->head->next;
}
while (temp->tail != NULL)
{
printf("%s \n", temp->tail->name);
temp->tail = temp->tail->prev;
}
}
int main(void)
{
FILE *fp;
char *line = NULL, *name=NULL, *flag=NULL;
size_t len = 0;
ssize_t read = 0;
struct List *list; // GDB says *list is not initialized. Even after initialization same fault.
list->head = list->tail = NULL;
/* start with empty list */
fp = fopen("data.txt", "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1)
{
name = strtok(line, " ");
flag = strtok(NULL, "\n");
if (strncmp(flag, "a", 1) == 0)
{
Insert(list, name);
}
else
{
Delete(list, name);
}
}
fclose(fp);
// Printing the List
printf("\n\n");
printf("/////////////////////\n");
printf("/// Print Forward ///\n");
printf("/////////////////////\n");
printForward(list);
// Printing the List
printf("\n\n");
printf("/////////////////////\n");
printf("/// Print Reverse ///\n");
printf("/////////////////////\n");
printReverse(list);
// Free Links
Free_list(list);
}
文字档案
Beverly a
Kathy a
Radell a
Gary a
Chuck a
David a
kari a
Tom a
Tanya a
Scott a
Beverly d
Brenda d
Kathy a
Gary a
WenChen a
Chuck a
Mike a
Emanuel a
Linda a
Bernie a
Hassan a
Brian a
Gary d
Kathy d
Gary a
Eunjin a
Kathy a
Brenda a
Jun a
Peanut a
Travis a
GDB错误
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400c1a in main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12- 1.149.el6.x86_64
答案 0 :(得分:6)
您的部分代码:
struct List *list; /* GDB says *list is not initialized.
Even after initialization same fault.*/
*list
指针未初始化(未分配内存),将其初始化为:
list = malloc(sizeof(struct List));
如果没有内存分配,则为Undefined behavior
未定义的行为可以解释问题“有时可行/有时无效”。请参阅上面的链接以获取有关未定义行为的更多信息