简体   繁体   中英

C program Error - Segmentation fault (core dumped)

I wanted to practice linked lists and so created a small program that would create a linked list. The program compiles fine using gcc but when i run the program, it works fine till line 13 in main() (scanf("%d", &value1)) after which it prints the error message - "segmentation fault (core dumped)". I added a print statement in my own testing after line 13 and it doesn't get printed (hence my assumption that the problem arrises after scanf which I dont know why).

So what am I doing wrong?

// Creates n numbers of nodes in a linked list where n is the argument 

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

#define MAX_NAME_LEN 25

typedef struct fruits {
    char *name;
    int price;
    struct fruits *next;
}fruits;

int check_format(int argc, char **argv);
fruits *create_node(char name[MAX_NAME_LEN], int value);
void print_nodes(fruits *head);

int check_format(int argc, char **argv) {

    if (argc == 2) {
        int argument = atoi(argv[1]);
        if (argument > 0) {
            return 1;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

fruits *create_node(char name[MAX_NAME_LEN], int value) {

    fruits *newNode = malloc(sizeof(fruits));
    strcpy(newNode->name, name);
    newNode->price = value;
    newNode->next = NULL;

    return newNode;
}

void print_nodes(fruits *head) {

    fruits * curr = head;
    while (curr != NULL) {
        printf("%s is %d\n", head->name, head->price);
        curr = curr->next;
    }
}

int main(int argc, char **argv) {

    int valid_format = check_format(argc, argv);

    fruits *curr = NULL;
    fruits *head = curr;
    if (valid_format) {
        int number_of_nodes;
        char fruitName[MAX_NAME_LEN] = {""};
        int value1 = 0;

        number_of_nodes = atoi(argv[1]);
        while (number_of_nodes) {

            printf("Enter the fruit name: ");
            scanf("%s", fruitName);

            printf("Enter the fruit price: ");
            scanf("%d", &value1);

            curr = create_node(fruitName, value1);
            curr = curr->next;
            number_of_nodes--;
        }
    } else {
        fprintf(stderr, "%s", "Usage: ./linked_lists [non-negative integer greater than 0]\n");
    }
    print_nodes(head);
    return 0;
}

You are getting segmentation fault because you are accessing newNode->name pointer without allocating memory to it in create_node() :

    strcpy(newNode->name, name);

Allocate memory to newNode->name and then copy name to it:

newNode->name = malloc(MAX_NAME_LEN);
if (newNode->name == NULL)
    exit (EXIT_FAILURE);

strcpy(newNode->name, name);

With this, make sure to free the memory allocated to name pointer before freeing the node.

As an alternative, you can have name as char array instead of pointer in structure fruits :

typedef struct fruits {
    char name[MAX_NAME_LEN];
    ....
    .... 

With this, you don't need separately allocate/deallocate memory to name .


Additional:

Follow good programming practice, always check the malloc return:

fruits *newNode = malloc(sizeof(fruits));
if (newNode == NULL)
    exit (EXIT_FAILURE);
.....
.....

Make sure to free the dynamically allocated memory once you are done with it. I do not see any where you are freeing the list nodes after printing them.

fruits *create_node(char name[MAX_NAME_LEN], int value)
{
    fruits *newNode = malloc(sizeof(fruits));
    strcpy(newNode->name, name);
    newNode->price = value;
    newNode->next = NULL;

    return newNode;
}

Here you allocate memory for the struct but not for the name. You have to either allocate memory for the name or change the type from pointer to a fixed size array:

typedef struct fruits 
{
    char name[MAX_NAME_LEN];
    int price;
    struct fruits *next;
}fruits;

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