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

struct node {
    int data;
    struct node *link;
};
void append (int data, struct node **s) {
    struct node *p, *q;
    if (*s == NULL) {
        p = (struct node *) malloc (sizeof (struct node));
        p->data = data;
        p->link = NULL;
        *s = p;
        return;
    }
    q = *s;
    p = (*s)->link;
    while (p != NULL) {
        q = p;
        p = p->link;
    }

    q->link = (struct node *) malloc (sizeof (struct node));
    q->link->data = data;
    q->link->link = NULL;
    return;
}

void insertion_sort (struct node *s) {
    int i, j;
    struct node *temp = s, *r;
    for (i = 0; temp != NULL; i++, temp = temp->link)
        ;
    int n = i;
    for (i = 0; i < n; i++) {
        r = s->link;
        for (j = i + 1; j < n ; j++)  {
            if (s->data > r->data) {
                int temp = s->data;
                s->data = r->data;
                r->data = temp;
            }
            r = r->link;
        }
        s = s->link;
    }
}
void display (struct node *p) {
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->link;
    }
    printf("\n");
}
main() {
    struct node *list1, *list2;
    append (56, &list1);
    append (45, &list1);
    append (99, &list1);
    append (23, &list1);
    append (54, &list1);

    display(list1);
    insertion_sort(list1);
    display(list1);

    append (44, &list2);
    append (12, &list2);
    append (57, &list2);
    append (76, &list2);
    append (89, &list2);

    display(list2);
    insertion_sort(list2);
    display(list2);
}

If I comment from where I start appending list2 in the main function, the code works fine for list1 and the list sorts properly.
I'm getting error "Segmentation fault(core dumped)" if I append numbers to list2.
Please someone explain me why I'm getting error when I'm making list2 ?
This code works fine:

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

struct node {
    int data;
    struct node *link;
};
void append (int data, struct node **s) {
    struct node *p, *q;
    if (*s == NULL) {
        p = (struct node *) malloc (sizeof (struct node));
        p->data = data;
        p->link = NULL;
        *s = p;
        return;
    }
    q = *s;
    p = (*s)->link;
    while (p != NULL) {
        q = p;
        p = p->link;
    }

    q->link = (struct node *) malloc (sizeof (struct node));
    q->link->data = data;
    q->link->link = NULL;
    return;
}

void insertion_sort (struct node *s) {
    int i, j;
    struct node *temp = s, *r;
    for (i = 0; temp != NULL; i++, temp = temp->link)
        ;
    int n = i;
    for (i = 0; i < n; i++) {
        r = s->link;
        for (j = i + 1; j < n ; j++)  {
            if (s->data > r->data) {
                int temp = s->data;
                s->data = r->data;
                r->data = temp;
            }
            r = r->link;
        }
        s = s->link;
    }
}
void display (struct node *p) {
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->link;
    }
    printf("\n");
}
main() {
    struct node *list1, *list2;
    append (56, &list1);
    append (45, &list1);
    append (99, &list1);
    append (23, &list1);
    append (54, &list1);

    display(list1);
    insertion_sort(list1);
    display(list1);

    /* append (44, &list2);
    append (12, &list2);
    append (57, &list2);
    append (76, &list2);
    append (89, &list2);

    display(list2);
    insertion_sort(list2);
    display(list2); */
}

Edited 4 Years Ago by prakhs

I'm getting error "Segmentation fault(core dumped)" if I append numbers to list2.

Obviously you have an invalid pointer somewhere. I'd wager that either you're not properly checking for NULL before dereferencing the pointer, or you neglected to set a link to NULL when creating the list. Either way the segmentation fault is telling you that you're accessing memory that isn't owned by the process.

To fix these errors you trace through the code in a debugger until it happens. From there you can see which pointer is invalid, and can then backtack to the point where the pointer is set (or should be set) to find the source.

I didn't really go over the code yet but how does it run for you when you change

struct node *list1, *list2;

to

struct node *list1 = NULL, *list2 = NULL;

?

Thanks! problem solved!

I expect that you'll be back in relatively short order with the same error and a different cause? Not that I'm attacking Gonbe for pointing out the problem this time, but you still need to learn how to trace access violations.

Edited 4 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.