Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

C - Segfault attempting to get the value of a node in a doubly linked list

I am attempting to write a doubly linked list containing "Person" structs, but my main program crashes during the "Get" function to return a Person from the list

Here's the list program:

linkedlist.c (Note, I am cutting some stuff, including where I freed the mallocs, but I am including CreateList() and insertAt() because they're the only parts of this used before Get() in main.c)

#include <stdlib.h>
#include <stdio.h>
#include "person.h"
#include "linkedlist.h"

List CreateList(){
    List list = malloc(sizeof(struct list));
    list->size = 0;
    list->first = NULL;
    list->last = NULL;
    return list;
}

...

void insertAt(List list, Person person, int pos) { 
    Node link = malloc(sizeof(Node));
    if (link == NULL){
        printf("Malloc failed
");
        return;
    }
    link->data = &person;
    link->next = NULL;
    link->previous = NULL;
    Node* current = list->first;
    if (pos > list->size || pos < 0){
        printf("Error - Invalid position
");
        return;
    }
    if (pos > list->size || pos < 0){
        printf("Error - Invalid position
");
        return;
    }
    if(list->size == 0){
        list->first = &link;
        list->size++;
        return;
    }
    if(pos == 0){
        link->next = *list->first;
        list->first = &link;
        list->size++;
        return;
    }
    if(pos == list->size){
        link->previous = *list->last;
        list->last = &link;
        list->size++;
        return;
    }
    for (int i = 0; i < pos; i++){
        current = &(*current)->next; 
    }
    link->next = (*current)->next;
    link->next->previous = link;
    link->previous = (*current)->previous;
    link->previous->next = link;
    return;
}

...

Person Get(List list, int pos){
    Person bob;
    Node* current = list->first;
    if (pos >= list->size || pos < 0){
        printf("Error - Invalid position");
        return bob;
    }
    if (IsEmpty(list)){
        printf("Error - List is empty");
        return bob;
    }
    for (int i = 0; i < pos; i++){
        current = &(*current)->next; 
    }
    Person* bobPtr = (*current)->data;
    bob = (Person) *bobPtr; // segfault happens here
    return bob;
}

Here are my header files:

linkedlist.h

#ifndef LIST_H
#define LIST_H

#include "person.h"

typedef struct node {
    Person* data;
    struct node* previous;
    struct node* next;
} *Node;

typedef struct list {
    Node* first;
    Node* last;
    int size;
} *List;

List CreateList();
void DestroyList(List*);
void CleanList(List);

void InsertFirst(List, Person);
void InsertLast(List, Person);
void insertAt(List, Person, int);

Person RemoveFirst(List);
Person RemoveLast(List);
Person RemoveAt(List, int);

Person First(List);
Person Last(List);
Person Get(List, int);

int IsEmpty(List);
int IsFull(List);
int Size(List);

# endif

person.h

#ifndef PERSON_H
#define PERSON_H

typedef struct person{
    int id;
    char name[20];
    int age;
} Person;

#endif
question from:https://stackoverflow.com/questions/65894783/c-segfault-attempting-to-get-the-value-of-a-node-in-a-doubly-linked-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...