Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:分段错误:在NetBeans(OS X)上运行,但在Linux上不运行_C_Linux_Netbeans_Linked List_Segmentation Fault - Fatal编程技术网

C:分段错误:在NetBeans(OS X)上运行,但在Linux上不运行

C:分段错误:在NetBeans(OS X)上运行,但在Linux上不运行,c,linux,netbeans,linked-list,segmentation-fault,C,Linux,Netbeans,Linked List,Segmentation Fault,我的程序使用基于指针的链表。它打开一个文本文件,添加(a)/删除(d)链接列表中指定的内容 该程序在Netbeans(MacOSX)上运行。但是当我在Linux(RHEL6.5)上运行这个程序时,我遇到了一个分段错误 我运行了gdb,并在下面发布了错误。任何帮助都将不胜感激 代码 #include<stdio.h> #include<stdlib.h> #include<string.h> struct Node { char name[42];

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

该程序在Netbeans(MacOSX)上运行。但是当我在Linux(RHEL6.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 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);

}
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 = malloc(sizeof(struct List));
如果没有内存分配,则为


未定义的行为可以解释问题“有时有效/有时无效”。请参阅上面的链接以获取有关未定义行为的更多信息

是否可以包括注释中提到的初始化?否,我指的是初始化
*列表
指针的源代码的一部分@user3337714@user3337714我仍然没有在main中看到
列表的初始化。请用修复的代码显示代码,因为这肯定是个问题。我刚刚发布了GDB错误。它现在只是简单地给出了分段错误。@user3337714我想您没有理解我们的请求。您说过“GDB说*列表未初始化。即使在初始化之后,也是相同的错误。”。请给我们看看代码更改。也就是说,实际的“初始化后”代码。我得到了以下错误
main.c:184:39:错误:使用了未声明的标识符'List';你是说“名单”吗?struct List*List=malloc(sizeof(List));^~~~listmain.c:184:18:注意:这里声明了struct list*list=malloc(sizeof(list))^
sizeof(*list)
sizeof(struct list)
没问题。这只是一个简单的打字错误。
list = malloc(sizeof(struct List));