Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    Segmentation fault

    Here is my code.

    While debugging i found out that a segmentation fault error while the third element 6 is added.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
        int data;
        struct node *link;
    };
    
    void display(struct node *q) {
        while(q != NULL) {
            printf("%d ",q -> data);
            q = q -> link;
        }
    }
    
    int count(struct node *q) {
        int c = 0;
    
        while(q != NULL) {
            c++;
            q = q -> link;
        }
        return c;
    }
    
    void add(struct node **q,int num) {
        struct node *r,*temp = *q;
        r = (struct node *)malloc(sizeof(struct node));
        r -> data = num;
    
        if(*q == NULL || num < (*q) -> data) {
            *q = r;
            r -> link = temp;
        }
        else {
            while(temp != NULL) {
                if(num >= temp -> data && (num < temp -> link -> data || temp -> link == NULL)) {
                    r -> link = temp -> link;
                    temp -> link = r;
                    return;
                }
                temp = temp -> link;
            }
        }
    }
    
    int main() {
        struct node *p = NULL;
    
        add(&p,5);
        add(&p,1);
        add(&p,6);
        add(&p,4);
        add(&p,7);
    
        display(p);
    }
    Please help guys

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    num < temp -> link -> data || temp -> link == NULL
    if temp->link is null the left condition will cause the crash
    you need to swap them
    Code:
    temp -> link == NULL || num < temp -> link -> data
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Whilst it's very good to put spaces either side of =, please don't do that for -> because it makes it very hard to read.
    Code:
    if (num >= temp->data && (temp->link == NULL || num < temp->link->data)) {
    FTFY!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM