简体   繁体   中英

Segmentation fault while doing malloc for a struct pointer

I trying to write basic Linked list code. I have two functions to add data to beginning of list and end of the list. Function to add data in the beginning works fine every time.

I am facing segmentation fault in insert_end function always. I am getting the error while trying to access temp1 pointer to structure after doing malloc to it. But this is the same exact thing I am doing even in the insert_first function, but works every time. I tried googling it and tried in forums, no answer. Please help.

This link is my exact kinda problem.. but i dont understand the solution fully

Segmentation fault in C with malloc

I am getting error particularly on this block

struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) { //******************Segmentation fault at this line**********
        temp1->data = input;
    }






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

//*******Structure to hold linked list*********//
struct node {
    int data;
    struct node *next;
}*head,*temp;


//***********Function to Display everything**********//

void display(struct node *head) {
    struct node *trav;
    trav= (struct node *)malloc(sizeof(struct node));
    printf("Entering into Display\n");
    if (head == NULL) {
        printf("Oh My God, the list is empty\n");
    }
    else {
        trav = head;
        while (trav != NULL) {
            printf("Value stored in [%p] is [%d]\n",trav,trav->data);
            trav = trav->next;
        }
    }
}


//***********Function to Insert at beginning*********//

struct node *insert_first(struct node *head,int input) {
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = input;
    printf("\nEntering insert first");
    if (head == NULL) {
        head = temp;
        head->next = NULL;
    }
    else {
        temp->next = head;
        head = temp;
    }
    return head;
}


//**************Function to Insert at End******************//
struct node *insert_last(struct node *head,int input) {
    struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) {
        temp1->data = input;
    }
    else {
        printf("empty");
    }
    printf("\nEntering insert last");
    if (head == NULL) {
        head = temp1;
        head->next = NULL;
    }
    else {
        trav = head;
        while (trav != NULL) {
            trav = trav->next;
        }
        trav->next = temp1;
    }
    return head;
}




//*************Main Fucntion***********//

int main() {
    int choice,value;
    head = NULL;
    while(1) {
        printf("\n******Please Enter your choice****\n1. To insert at beginning\n2. To insert at End\n3. To Insert middle\n4. To delete\n5. To display\n0. To Exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_first(head,value);
                break;
            case 2:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_last(head,value);
                break;

            case 5:
                display(head);
                break;
            case 0:
                return 0;
            default:
                printf("Thats a wrong choice\n");
                break;
        }
    }
}

This is the problematic block.

else {
    trav = head;
    while (trav != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;

When you come out of the while loop trav is NULL . That makes the line

    trav->next = temp1;

fail with segmentation violation.

Change that block to:

else {
    trav = head;
    while (trav->next != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM