双向链表C实现运行时错误

时间:2022-01-30 06:05:42

Below is the C program I have written. It contains an implementation of the doubly linked list.

下面是我写的C程序。它包含双向链表的实现。

#include <stdio.h>

/* node of a doubly linked list */
typedef struct _dlnode {
    struct _dlnode* prev;
    int key;
    struct _dlnode* next;
} dlnode;

/* doubly linked list */
typedef struct _dllist {
    dlnode* head;
    dlnode* tail;
} dllist;

/* returns an empty doubly linked list */
dllist* empty_dllist () {
    dllist* l;
    l->head=NULL;
    l->tail=NULL;
    return l;
}

int main()
{
    dllist* l;
    l=empty_dllist ();
    return 0;
}

I get the following runtime error:

我收到以下运行时错误:

Segmentation fault: 11

What is it caused by?

它是由什么引起的?

3 个解决方案

#1


1  

You have to allocate memory for a structure before you use a pointer to it to access its members. Change your function empty_dllist to -

在使用指向它的指针访问其成员之前,必须为结构分配内存。将你的函数empty_dllist更改为 -

dllist *empty_dllist(void) {
    dllist *l = malloc(sizeof *l);
    if(l == NULL) {
        // failed to allocate memory
        // handle it
        // return NULL
    }
    l->head = NULL;
    l->tail = NULL;
    return l;
}

#2


0  

A segmentation fault is usually caused by trying to follow an uninitialized or NULL pointer.

分段错误通常是由于尝试跟踪未初始化或NULL指针引起的。

In your program, you have pointer variable l in the function empty_dllist, and you try to follow that pointer to what it points to. But the variable is uninitialized, and cointains garbage, so it is not surprising that you get a segmentation fault.

在你的程序中,你在函数empty_dllist中有指针变量l,你试着按照它指向它的指针。但是变量是未初始化的,并且是垃圾,所以你得到分段错误并不奇怪。

You probably want to add a call to malloc in empty_dllist, to allocate a header struct for your list.

您可能希望在empty_dllist中添加对malloc的调用,以便为列表分配头结构。

#3


0  

You are not allocating memory:

你没有分配内存:

dllist* l = malloc(sizeof(dllist));

so trying to access l->head causes error memory access

所以试图访问l-> head导致错误内存访问

#1


1  

You have to allocate memory for a structure before you use a pointer to it to access its members. Change your function empty_dllist to -

在使用指向它的指针访问其成员之前,必须为结构分配内存。将你的函数empty_dllist更改为 -

dllist *empty_dllist(void) {
    dllist *l = malloc(sizeof *l);
    if(l == NULL) {
        // failed to allocate memory
        // handle it
        // return NULL
    }
    l->head = NULL;
    l->tail = NULL;
    return l;
}

#2


0  

A segmentation fault is usually caused by trying to follow an uninitialized or NULL pointer.

分段错误通常是由于尝试跟踪未初始化或NULL指针引起的。

In your program, you have pointer variable l in the function empty_dllist, and you try to follow that pointer to what it points to. But the variable is uninitialized, and cointains garbage, so it is not surprising that you get a segmentation fault.

在你的程序中,你在函数empty_dllist中有指针变量l,你试着按照它指向它的指针。但是变量是未初始化的,并且是垃圾,所以你得到分段错误并不奇怪。

You probably want to add a call to malloc in empty_dllist, to allocate a header struct for your list.

您可能希望在empty_dllist中添加对malloc的调用,以便为列表分配头结构。

#3


0  

You are not allocating memory:

你没有分配内存:

dllist* l = malloc(sizeof(dllist));

so trying to access l->head causes error memory access

所以试图访问l-> head导致错误内存访问