strcpy上的分段错误,即使我使用malloc

strcpy上的分段错误,即使我使用malloc,c,segmentation-fault,malloc,strcpy,C,Segmentation Fault,Malloc,Strcpy,我想用C语言制作一个双链表 即使我使用malloc,也会出现分割错误 这是到目前为止我的代码 list.h #ifndef _LIST_ #define _LIST_ typedef struct listnode { char * data; struct listnode * next; struct listnode * prev; }listnode; typedef struct list { listnode * firstnode; // it

我想用C语言制作一个双链表

即使我使用malloc,也会出现分割错误

这是到目前为止我的代码

list.h

#ifndef _LIST_
#define _LIST_

typedef struct listnode
{
    char * data;
    struct listnode * next;
    struct listnode * prev;
}listnode;

typedef struct list
{
    listnode * firstnode; // it will point to the first element in the list
    int size; // the size of the list
}list;

list create_list();
void insert_first_element(char *, list);

#endif
#ifndef LIST_H
#define LIST_H

typedef struct listnode {
    char *data;
    struct listnode *next;
    struct listnode *prev;
} listnode;

typedef struct list {
    listnode *firstnode; // it will point to the first element in the list
    int size; // the size of the list
} list;

list create_list(void);
void insert_first_element(list *, const char *);
#endif
list.c

#include "list.h"
#include <stdlib.h>
#include <string.h>

list create_list()
{
    list L;
    L.firstnode= NULL;
    L.size = 0;

    return L;
}

void incert_first_element(char * d, list L)
{
    listnode * N= (listnode *)malloc(sizeof(listnode));
    
    strcpy(N->data, d); // <-- I get Segmentation Fault Here

    if(L.firstnode != NULL)
    {
        N->next=L.firstnode;
        N->prev=L.firstnode->prev;
        L.firstnode->prev=N;
        L.firstnode=N;
    }
    else
    {
        N->next=NULL;
        N->prev=N;
        L.firstnode=N;
    }
    L.size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void)
{
   list L = create_list();
   incert_first_element("test",L);
  
   return 0;
}
#include <stdlib.h>
#include <string.h>
#include "list.h"

list create_list(void) {
    list L = { NULL, 0 };
    return L;
}

void insert_first_element(list *L, const char *d) {
    listnode *N = malloc(sizeof(*N));
    if (N == NULL) {
        return -1;
    }
    N->data = strdup(d);
    N->prev = NULL;
    N->next = L->firstnode;

    if (L->firstnode != NULL) {
        L->firstnode->prev = N;
    }
    L->firstnode = N;
    L->size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void) {
   list L = create_list();
   insert_first_element(&L, "test");
  
   return 0;
}
#包括“list.h”
#包括
#包括
列表创建_列表()
{
清单L;
L.firstnode=NULL;
L.尺寸=0;
返回L;
}
第一个元素无效(字符*d,列表L)
{
listnode*N=(listnode*)malloc(sizeof(listnode));
strcpy(N->data,d);//next=L.firstnode;
N->prev=L.firstnode->prev;
L.firstnode->prev=N;
L.firstnode=N;
}
其他的
{
N->next=NULL;
N->prev=N;
L.firstnode=N;
}
L.size++;
返回0;
}
main.c

#include "list.h"
#include <stdlib.h>
#include <string.h>

list create_list()
{
    list L;
    L.firstnode= NULL;
    L.size = 0;

    return L;
}

void incert_first_element(char * d, list L)
{
    listnode * N= (listnode *)malloc(sizeof(listnode));
    
    strcpy(N->data, d); // <-- I get Segmentation Fault Here

    if(L.firstnode != NULL)
    {
        N->next=L.firstnode;
        N->prev=L.firstnode->prev;
        L.firstnode->prev=N;
        L.firstnode=N;
    }
    else
    {
        N->next=NULL;
        N->prev=N;
        L.firstnode=N;
    }
    L.size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void)
{
   list L = create_list();
   incert_first_element("test",L);
  
   return 0;
}
#include <stdlib.h>
#include <string.h>
#include "list.h"

list create_list(void) {
    list L = { NULL, 0 };
    return L;
}

void insert_first_element(list *L, const char *d) {
    listnode *N = malloc(sizeof(*N));
    if (N == NULL) {
        return -1;
    }
    N->data = strdup(d);
    N->prev = NULL;
    N->next = L->firstnode;

    if (L->firstnode != NULL) {
        L->firstnode->prev = N;
    }
    L->firstnode = N;
    L->size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void) {
   list L = create_list();
   insert_first_element(&L, "test");
  
   return 0;
}
#包括
#包括“list.h”
内部主(空)
{
列表L=创建_列表();
输入第一个元素(“测试”,L);
返回0;
}
知道是什么导致了分割错误吗

因为我在谷歌搜索时发现的任何问题都是由于缺少malloc造成的,但在这里我实现了它。

这段代码

listnode * N= (listnode *)malloc(sizeof(listnode));

strcpy(N->data, d); // <-- I get Segmentation Fault Here

取消分配也应在两次过程中完成。首先
free(N->data)
然后
free(N)

您的代码中存在多个问题:

  • incert\u first\u元素
    接收
    列表
    结构的副本并对其进行修改。这对调用者的
    列表
    对象没有影响。您应该将指针传递给调用方的
    列表
    对象

  • incert\u first\u元素
    函数分配一个新的
    listnode
    对象,但不分配给字符串。成员
    data
    是指针,而不是数组,
    malloc()
    不初始化它,因此
    strcpy(N->data,d)将字符复制到未初始化的指针中,调用未定义的行为(使程序弯曲的分段错误)。您应该使用
    N-strcpy(N->data,d)分配字符串的副本

  • 在双链表的开头插入节点,因此设置
    N->prev=N是不正确的
    在这两种情况下,前一个节点都应设置为
    NULL

  • list.c中,您应该在标准标题后添加“list.h”

以下是修改后的版本:

list.h

#ifndef _LIST_
#define _LIST_

typedef struct listnode
{
    char * data;
    struct listnode * next;
    struct listnode * prev;
}listnode;

typedef struct list
{
    listnode * firstnode; // it will point to the first element in the list
    int size; // the size of the list
}list;

list create_list();
void insert_first_element(char *, list);

#endif
#ifndef LIST_H
#define LIST_H

typedef struct listnode {
    char *data;
    struct listnode *next;
    struct listnode *prev;
} listnode;

typedef struct list {
    listnode *firstnode; // it will point to the first element in the list
    int size; // the size of the list
} list;

list create_list(void);
void insert_first_element(list *, const char *);
#endif
list.c

#include "list.h"
#include <stdlib.h>
#include <string.h>

list create_list()
{
    list L;
    L.firstnode= NULL;
    L.size = 0;

    return L;
}

void incert_first_element(char * d, list L)
{
    listnode * N= (listnode *)malloc(sizeof(listnode));
    
    strcpy(N->data, d); // <-- I get Segmentation Fault Here

    if(L.firstnode != NULL)
    {
        N->next=L.firstnode;
        N->prev=L.firstnode->prev;
        L.firstnode->prev=N;
        L.firstnode=N;
    }
    else
    {
        N->next=NULL;
        N->prev=N;
        L.firstnode=N;
    }
    L.size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void)
{
   list L = create_list();
   incert_first_element("test",L);
  
   return 0;
}
#include <stdlib.h>
#include <string.h>
#include "list.h"

list create_list(void) {
    list L = { NULL, 0 };
    return L;
}

void insert_first_element(list *L, const char *d) {
    listnode *N = malloc(sizeof(*N));
    if (N == NULL) {
        return -1;
    }
    N->data = strdup(d);
    N->prev = NULL;
    N->next = L->firstnode;

    if (L->firstnode != NULL) {
        L->firstnode->prev = N;
    }
    L->firstnode = N;
    L->size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void) {
   list L = create_list();
   insert_first_element(&L, "test");
  
   return 0;
}
#包括
#包括
#包括“list.h”
列表创建_列表(无效){
列表L={NULL,0};
返回L;
}
void insert_第一个元素(list*L,const char*d){
listnode*N=malloc(sizeof(*N));
如果(N==NULL){
返回-1;
}
N->data=strdup(d);
N->prev=NULL;
N->next=L->firstnode;
如果(L->firstnode!=NULL){
L->firstnode->prev=N;
}
L->firstnode=N;
L->size++;
返回0;
}
main.c

#include "list.h"
#include <stdlib.h>
#include <string.h>

list create_list()
{
    list L;
    L.firstnode= NULL;
    L.size = 0;

    return L;
}

void incert_first_element(char * d, list L)
{
    listnode * N= (listnode *)malloc(sizeof(listnode));
    
    strcpy(N->data, d); // <-- I get Segmentation Fault Here

    if(L.firstnode != NULL)
    {
        N->next=L.firstnode;
        N->prev=L.firstnode->prev;
        L.firstnode->prev=N;
        L.firstnode=N;
    }
    else
    {
        N->next=NULL;
        N->prev=N;
        L.firstnode=N;
    }
    L.size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void)
{
   list L = create_list();
   incert_first_element("test",L);
  
   return 0;
}
#include <stdlib.h>
#include <string.h>
#include "list.h"

list create_list(void) {
    list L = { NULL, 0 };
    return L;
}

void insert_first_element(list *L, const char *d) {
    listnode *N = malloc(sizeof(*N));
    if (N == NULL) {
        return -1;
    }
    N->data = strdup(d);
    N->prev = NULL;
    N->next = L->firstnode;

    if (L->firstnode != NULL) {
        L->firstnode->prev = N;
    }
    L->firstnode = N;
    L->size++;
    return 0;
}
#include <stdio.h>
#include "list.h"

int main(void) {
   list L = create_list();
   insert_first_element(&L, "test");
  
   return 0;
}
#包括
#包括“list.h”
内部主(空){
列表L=创建_列表();
插入第一个元素(&L,“测试”);
返回0;
}

是的,很明显,
N->data
并不是为您的信息而初始化的,您的include-guard可能不应该以下划线开头:OP是按值传递列表的,因此insert函数在返回时不会起作用。Arg应该是
List*L
,正文中有
L.
-->
L->