C: Segmentation Fault:在NetBeans (OS X)上运行,在Linux上不运行



我的程序使用一个基于指针的链表。它打开一个文本文件,添加(a)/删除(d)指定的内容到链表。

程序运行在Netbeans (MAC OS X)上,但是当我在Linux (RHEL 6.5)上运行程序时,我得到一个分段错误。

我运行gdb并得到下面发布的错误。如有任何帮助,不胜感激。

#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 listn", string);
            return;
        }
        else
        {
            curr = curr->next;
        }
    }
    if (curr == NULL)
    {
        printf("%s is not in the listn", 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("%sn", 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("nn");
    printf("/////////////////////n");
    printf("/// Print Forward ///n");
    printf("/////////////////////n");
    printForward(list);
    // Printing the List
    printf("nn");
    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

您的代码部分:

struct List *list; /* GDB says *list is not initialized.
                      Even after  initialization same fault.*/

*list指针没有初始化(没有分配内存),像这样初始化它:

list = malloc(sizeof(struct List));

如果没有内存分配,则为未定义行为

未定义的行为可以解释"sometimes works/sometimes not"的问题。查看上面的链接获取更多关于未定义行为的信息

相关内容

  • 没有找到相关文章

最新更新