0
votes

I'm using cygwin, installed gcc-g++ to compile c written using notepad++.

I want to create a linked list to store data, but it always reports "Segmentation fault (core dumped)"

I figured out that if I put "printf("OK")" after the "scanf" in the 4th row of the main function it doesn't work but shows Segmentation fault again.

Is this a memory management problem? or Pointer abuse?

Here is my code:

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

struct Node{
    int data;
    struct Node *link;
}node;

typedef struct Node *nodePointer;


nodePointer GetNewNode(){           // create a new node
    nodePointer NewNode;    
    NewNode = (nodePointer) malloc(sizeof(node)); 
    if (NewNode == NULL)
        printf("memery is not enough");
    return NewNode;
}

nodePointer insert(nodePointer ptr,nodePointer L,int NewData){  
    nodePointer NewNode,trace;
    NewNode = GetNewNode();
    if(ptr==NULL){                  //to insert first Node
        ptr->link = NewNode;
        NewNode->link = NULL;
        NewNode->data = NewData;
        L = ptr;
    }
    else{                           //after insert first Node,insert next Node
        trace = L;
        while(trace->link!=NULL) trace = trace->link;
        trace->link = NewNode;
        NewNode->data = NewData;
    }
    return L;
}

 int main(){
    int data;
    nodePointer ptr=NULL,L=NULL,visit=NULL;
    printf("input data to storage, input 0 to print data\n");
    scanf("%d",&data);
    while(data!=0){                 // input data to store, input 0 to print data
        L = insert(ptr,L,data);
        scanf("%d",&data);
    }
    visit = L;
    while(visit->link!=NULL)
        printf("%d",visit->data);
}
2
undefined behaviour is undefined.. use a debugger ( gdb, etc. ) to identify the problem and post the details - amdixon
typedef struct Node *nodePointer; Never hide pointers behind typedefs, that's bad style as it makes the program harder to read and understand. - Lundin
I get it , thanks for you guys instruction ! - Guan
except for the silly typedef: ... = malloc(sizeof(node)); works by coincidence (unless the OP uses a C++ compiler, which he should not) - wildplasser
Use gcc to compile C , and g++ to compile C++. They are different languages. - M.M

2 Answers

0
votes

In your code you have this:

if (ptr==NULL){
    // ptr is NULL here and right after you dereference ptr
    // so it's normal that you get a segfault
    ptr->link = NewNode;

There are probably more issues here.

0
votes

After correct , work well.

#include <stdio.h>
#include <malloc.h>

typedef struct Node *nodePointer;
struct Node{
    int data;
    nodePointer link;
}node;

nodePointer GetNewNode(){
    nodePointer NewNode;    
    NewNode = (nodePointer) malloc(sizeof(node));  
    if (NewNode == NULL)
        printf("memery is not enough");
    return NewNode;
}

nodePointer insert(nodePointer L,int NewData){
    nodePointer NewNode,trace;
    if(L==NULL){                
        L = (nodePointer) malloc(sizeof(node));
        L->data = NewData;
        L->link = NULL;
    }
    else{                       
        malloc(sizeof(node)); there no necessaryry
        trace = L;
        while(trace->link!=NULL) trace = trace->link;
        NewNode = GetNewNode();
        trace->link = NewNode;
        NewNode->data = NewData;
        NewNode->link = NULL;
    }
    return L;
}

 int main(){
    int data;
    nodePointer L=NULL,visit=NULL;
    printf("input data to storage, input -1 to print data\n");
    while(data!=-1){                
        scanf("%d",&data);
        if(data==1) break;
        L = insert(L,data);
    }                           
    visit = L;
    while(visit!=NULL){
        printf("%d",visit->data);
        visit = visit->link;
    }
}