0

When I run this code it shows a Segmentation fault.

I have searched the other related post on Stackoverflow but didn't get the answer or why my code showing this error.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
    int info;
    struct node *next;
};
typedef struct node NODE;


NODE* getNode();
void insertAtFirst(NODE*,int);
void traverse(NODE*);

int main(){
    NODE *start = NULL;
    insertAtFirst(start,1);
    insertAtFirst(start,4);
    traverse(start);
    return 0;
}

void insertAtFirst(NODE *start, int n){
    NODE *p = (NODE*)malloc(sizeof(NODE));
    p->info = n;
    if(start == NULL){
        p->next = NULL;
    }
    else{
        p->next = start;
    }
    start = p;
}

void traverse(NODE *start){
    NODE *temp;
    temp = start;
    while(temp != NULL){
        printf("%d  ", temp->info);
        temp = temp->next;
    }
}

Please suggest me why I am getting a Segmentation fault (core dump) on running the program.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
mia
  • 1,148
  • 1
  • 11
  • 17
  • 1
    Please run it with the debugger to find out where the segmentation fault occurs – Ed Heal Feb 23 '17 at 06:21
  • 1
    You are using `*start` as a single pointer, but in the context you have used it, it needs to be a a double. – Inian Feb 23 '17 at 06:21
  • Also you should not cast the return values from `malloc` – Ed Heal Feb 23 '17 at 06:22
  • 1
    @mia Probably not your problem but: `if (start == NULL) p->next = NULL; else p->next = start;`can be reduced to `p->next = start;` without change of behavior. – Scheff's Cat Feb 23 '17 at 06:23
  • If you're not casting in `NODE *start = NULL;` then I beg to question, why are you casting in `NODE *p = (NODE*)malloc(sizeof(NODE));`? [You shouldn't](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – autistic Feb 23 '17 at 06:23
  • Oh and... [Valgrind](http://valgrind.org/) is your friend! – autistic Feb 23 '17 at 06:24
  • @mia The actual problem is you provide the pointer `*start`by value. Thus, it is lost when you return from `insertAtFirst()`. Hence the hint with the debugging... – Scheff's Cat Feb 23 '17 at 06:26
  • Possible duplicate: [Dynamic memory access only works inside function](http://stackoverflow.com/questions/39486797/dynamic-memory-access-only-works-inside-function). – Lundin Feb 23 '17 at 09:03

2 Answers2

8

You should compile with all warnings & debug info (e.g. gcc -Wall -g if using GCC....). Then improve your code to get no more warnings.

Then you should use the debugger, e.g. gdb.

I won't correct your code but you need to understand that all arguments (including pointers) are passed by value. So if a pointer is passed to some function which modifies its argument, the original pointer stay unchanged.

You should spend several days reading good books about C and about programming (and debugging and testing). You should also read some existing source code, e.g. from some free software project.

BTW, ability to use the debugger is a required skill when coding in C (or in C++, BTW). So read documentation of gdb and tutorials about it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • This should be the canonical answer to my code segfaults, and close as duplicate. – dave Feb 23 '17 at 06:31
  • @dave: not every segfault in homeworks are because of misunderstanding on argument passing. Some newbie students are able of other kind of bugs. But yes, every student should learn debugging. I can't understand why their teacher didn't taught that. – Basile Starynkevitch Feb 23 '17 at 06:43
  • yes, but you didn't tell this person why this particular code seg faulted, you explained how to find it! Which is always the answer to "why does this seg fault?", but perhaps not the answer to some other more thoroughly thought out question after trying the above. – dave Feb 23 '17 at 08:42
  • 2
    @dave We already have 2 canonical posts about "generic segfaults". [What is a segmentation fault?](http://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) and [Definitive List of Common Reasons for Segmentation Faults](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults). – Lundin Feb 23 '17 at 09:05
5
void insertAtFirst(NODE *start, int n){
    NODE *p = malloc(sizeof(NODE));
    p->info = n;
    if(start == NULL){
        p->next = NULL;
    }
    else{
        p->next = start;
    }
    start = p;
}

The last line start = p doesn't do what you think it does, it doesn't make the original pointer inside main point to the new node, since inside the function, start is a copy of the original pointer passed to this function.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90