Looking for the origin of segmentation fault.

Asked 1 years ago, Updated 1 years ago, 63 views

Arrange named and numbered structures in a linked list After searching with a keyword, I wrote the name with the goal of printing the number or vice versa. It's my first time writing based on linux, so I think I paid too much attention to the outside of the code.

A compilation error was almost caught, but as the title suggests, it stopped with only a dump. I'm not used to using debugging tools, so it's hard to accept the exact meaning of the error message.

Program received signal SIGSEGV, Segmentation fault. 0x00010654 in add_node()

If it comes out like this, is there a problem as add_node() is executed in main.c? Or is there an error in the implementation of the add_node() function itself?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct _Student{
    char name[10];
    char number[10];
} } Student;

typedef struct node{
    Student* Std;
    struct node* next;
}NODE;

typedef struct{
    int count;
    NODE* front;
    NODE* rear;
    NODE* pos;
}LLIST;

LLIST* create_list(){
    LLIST* list;
    list = (LLIST*)malloc(sizeof(LLIST));
    if(list){
        list->front = NULL;
        list->rear = NULL;
        list->count = 0;
    }
    return list;
}

void add_node(LLIST* list,  Student* Std_info) {
    NODE* new_node=(NODE*)malloc(sizeof(NODE));

    new_node -> Std = Std_info;
    new_node -> next = NULL;
    if(list -> count == 0){
        list->front = new_node;
        list->rear = new_node;
        (list->count)++;
        return;
    }
    int index = 0;
    list->pos = list->front;
    while(index != (list->count)){
        list->pos = list->pos->next;
        index++;
    }
    //Index value equals list->count at escape time

    list->pos->next = new_node;
    list->rear = new_node;
    (list->count)++;
    There is no error even if there is no return value when returning the //void type.
}

void del_node(LLIST* list){
    if(list->count == 1){
        free(list->front);
        list->front = NULL;
        list->rear = NULL;
        list->count = 0;
        return;
    }
    int index = 0;
    list->pos = list->front;
    NODE* bckup = NULL;
    while(index != list->count){
        bckup = list->pos;
        list->pos = list->pos->next;
        index++;
    }
    //Index value = list->count value at the time of escape
    list->rear = bckup;
    bckup->next = NULL;
    free(list->pos);
    list->pos=NULL;
    (list->count)--;
    return;
}
char* finderLLIST(LLIST* list, char* keyword){
    if(list->count==0){
        keyword = "empty list";   
        return keyword;
    }   
    int index = 0; 
    list->pos = list->front; //start from the front

    The //&& operator does not calculate the terms behind it if the result value is clear from the beginning.
    while(index < list->count && strcmp(list->pos->Std->name,keyword) != 0 && strcmp(list->pos->Std->number,keyword) != 0){ 
        list->pos = list->pos->next;
        index++;
    }

    if(index >= list->count){
        keyword = "Unmatched";
    }else if(strcmp(list->pos->Std->name,keyword) == 0){
        keyword = list->pos->Std->number;
    }else if(strcmp(list->pos->Std->number,keyword) == 0){
        keyword = list->pos->Std->name;
    }

    return keyword;
 }


char* finderARR(Student* Std, char* keyword){
    int index;

    for(index=0; index<=9; index++){
        if(strcmp(Std->name,keyword) == 0){
            keyword = (Std+index)->number;
            return keyword;
        }else if(strcmp((Std+index)->number,keyword) == 0){
            keyword = (Std+index)->name;
            return keyword;
        }
    }
    keyword = "Unmatched";
    return keyword;
}
int main(void){
    Student Std[10]={
    {.name = "Kim-Kim", .number = "1234123"},
    {.name = "sir", .number = "2222222"},
    {.name = "tread, tread", .number = "3333333"},
    {.name = "Rim Forest", .number   = "4444444"},
    {.name = "Memime", .number = "5555555"},
    {.name = "bibim", .number = "666666666"},
    {.name = "Bored", .number   = "7777777"},
    {.name = "Arbitrary", .number   = "8888888"},
    {.name = "Luggage", .number   = "9999999"},
    {.name = "Chimchimchim", .number   = "0000000"}
    };
    int i;

    Student Std_list[10] = {
    {.name = "Kim-Kim", .number = "1234123"},
    {.name = "sir", .number = "2222222"},
    {.name = "tread, tread", .number = "3333333"},
    {.name = "Rim Forest", .number   = "4444444"},
    {.name = "Memime", .number = "5555555"},
    {.name = "bibim", .number = "666666666"},
    {.name = "Bored", .number   = "7777777"},
    {.name = "Arbitrary", .number   = "8888888"},
    {.name = "Luggage", .number   = "9999999"},
    {.name = "Chimchimchim", .number   = "0000000"}
    };
    LLIST* list_Std = create_list();
    for(i=0; i<10; i++){
        add_node(list_Std, &Std_list[i]);
    }

    //So far, declaration of array and connection list and memory allocation and initialization.
    char keyword[10];

    printf ("Search in array: ");
    scanf("%s", keyword);
    //printf("\nCheck buffer 확인%s ㅁ", keyword);
    printf("-->%s \n", finderARR(Std, keyword));


    printf("Search in connection list: ");
    scanf("%s", keyword);
    //printf("\nCheck buffer 확인%s ㅁ", keyword);
    printf("-->%s", finderLLIST(list_Std, keyword));



    //Unallocate memory from array and connection list from here
    for(i=0; i<10; i++){
        del_node(list_Std);
    } 

    free(list_Std);

    return 0;
}

c runtime-error

2022-09-21 11:09

1 Answers

https://code.visualstudio.com/docs/cpp/config-linux#_debug-helloworldcpp

If you install vs code and follow the link above to set up build and debug, I think you can debug within vs code a little more comfortably. Try following me.

The capture below is an exception, following Ubuntu's settings and returning to debug mode. The list->pos value is NULL.


2022-09-21 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.