-2

What are some of the reasons that segmentation faults occur? In the following code I am trying to create an easy way to use linked lists. Basically in my program you just create a list using the linkedList struct type. However, there is a line in the program that causes a segmentation fault. Why does this happen? Any help would be much appreciated. Thank you :)

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

struct node{
    int num;
    struct node *next;
};
//Make the struct

typedef struct {
    struct node * first;
    struct node * current;
}linkedList;

void addNode(linkedList list, int a);
void addFirstNode(linkedList list, int b);
//Function prototypes

int main() {

    linkedList list;
    addFirstNode(list, 1);
    addNode(list, 2);
    addNode(list, 3);
    addNode(list, 4);
    addNode(list, 5);
    addNode(list, 6);
    addNode(list, 7);
}

void addFirstNode(linkedList list, int input) { 

    list.first = (struct node *)malloc(sizeof(struct node));   //Make first node
    list.first -> num = input;                                 //Fill first node
    list.current = list.first;                                     
}
void addNode(linkedList list, int input) {

   struct node * newNode = (struct node *)malloc(sizeof(struct node)); 
   newNode -> num = input;                      
   list.current -> next = newNode;    //Segmentation fault! (core dumped)
   list.current = newNode;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
PickleDonk
  • 75
  • 1
  • 8
  • 1
    which is the line of code that causes segmentation fault? – Satheesh Jul 25 '17 at 02:49
  • 1
    C does not have a concept of segmanttion fault. Undefined behaviour behaves - by definition - undefined. We are not a debugging service, read [ask]. Just that: your function interfaces are broken. C ist strictly pass-by-value. – too honest for this site Jul 25 '17 at 02:51
  • [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Jul 25 '17 at 02:52
  • `addFirstNode` receives a copy of `list` at `main` as its first argument. So `list`(at main) will not be changed. So In `addNode` function, It will use an uninitialized variable. Modify the code to receive a pointer. – BLUEPIXY Jul 25 '17 at 02:52
  • 1
    You have to pass the `list` by reference to `addFirstNode(linkedList *list, int input)` and `addNode(linkedList *list, int input)` otherwise after this `addFirstNode(list, 1);` `list.current` is still not initialise and accessing its member current is UB causing `Segmentation fault!` – Seek Addo Jul 25 '17 at 02:57
  • A segmentation fault is UNIX-like operating system specific (on Windows it is usually a Memory Access Violation). Basically it means you have a duff pointer. – cdarke Jul 25 '17 at 06:52
  • @SeekAddo Thanks! That was really helpful. I never considered the fact that C is a pass-by-value. – PickleDonk Jul 25 '17 at 21:40
  • @BLUEPIXY Thanks for the comment! That explains a lot :) – PickleDonk Jul 25 '17 at 21:41

1 Answers1

0

As pointed out in the comments, there are multiple things to correct in your code:

  • You need to pass the function arguments by reference (C passes elements by value).
  • You need to initialize your linked-list, otherwise you will be attempting to dereference a NULL pointer.

Here's a correct version of the code:

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

struct node{
    int num;
    struct node *next;
};
//Make the struct

typedef struct {
    struct node * first;
    struct node * current;
}linkedList;

void addNode(linkedList* list, int a);
void addFirstNode(linkedList* list, int b);
//Function prototypes

int main() {

    linkedList *list = (struct node *)malloc(sizeof(struct node));;
    addFirstNode(list, 1);
    addNode(list, 2);
    addNode(list, 3);
    addNode(list, 4);
    addNode(list, 5);
    addNode(list, 6);
    addNode(list, 7);
}

void addFirstNode(linkedList* list, int input) {

    list->first = (struct node *)malloc(sizeof(struct node));   //Make first node
    list->first->num = input;                                 //Fill first node
    list->current = list->first;
}
void addNode(linkedList *list, int input) {

    struct node * newNode = (struct node *)malloc(sizeof(struct node));
    newNode->num = input;
    list->current->next = newNode;    //Segmentation fault! (core dumped)
    list->current = newNode;
}
Ryan B.
  • 1,270
  • 10
  • 24