Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Segmentation fault

    Below is the code for segregating odd and even nodes of linked list such that the even nodes come before odd nodes.(e.g. for list 1->3->2->5->4->6 the output should be 2->4->6->1->3->5)


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
     
    struct node
    {
      int data;
      struct node * next;
    };
     typedef struct node *NODEPTR;
    void addElement(struct node **head, int x);
    int delNode(NODEPTR *);
    void printList(struct node * head);
    struct node * segregateOddAndEven(struct node *head);
     
    int main()
    {
      NODEPTR head=NULL;
      
      addElement(&head, 10);
      addElement(&head, 5);
      addElement(&head, 7);
      addElement(&head, 12);
      addElement(&head, 4);
      
     
      printList(head);
      
     head= segregateOddAndEven(head);
     printList(head);
      
      
    }
     
    void addElement(NODEPTR *head, int x)
    {
        
      NODEPTR newNode,p;
      newNode=(struct node*)malloc(sizeof(struct node));
      
      
      if(*head==NULL)
       {
         newNode->data=x;
        *head=newNode;
        (*head)->next=NULL;
       }
      else
      {
       p=*head;
       while(p->next)
        p=p->next;
        
        newNode->data=x; 
        newNode->next=p->next;
        p->next=newNode;
      } 
        
     }
     
     void printList(struct node *head)
     {
       while(head)
       {
        printf("%d ",head->data);
        head=head->next;
       }
       
     }
     
     int delNode(NODEPTR * node)
     {
         NODEPTR p;
         int x;
         p=*node;
         
         *node=(*node)->next;
         x=p->data;
         free(p);
         return x;
     }
     
    struct node * segregateOddAndEven(struct node *headRef)
    {
      struct node *odd=NULL,*temp,**p,*prev=NULL;
      p=&headRef;
      while(1)
      {
             
       if(((*p)->data%2)!=0 && *p)
       {
         
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=(*p)->data;
        temp->next=NULL;
        if(odd==NULL)
         odd=temp;
        else
          prev->next=temp; 
        prev=temp; 
        delNode(p);
       }
       else
       {
           if(!*p) 
          
            break;
            
        p=&((*p)->next);
       }
       
       
      }
      (*p)->next=odd;
      return headRef;
    }


    While running I am getting segmentation fault. Please let me know what is going wrong...
    Last edited by Salem; 03-21-2012 at 12:32 AM. Reason: fixed font eyesore

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you tried running it in the debugger?

    Using this, you'll find the exact line of code where it crashes (so you'll know more than "it doesn't work").
    Plus, you'll be able to examine all your variables to help you figure out why the pointer you tried to dereference is bad.

    The debugger is a vital tool to learn how to use, and now is a really good time to start that learning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think this belongs in C and not C++. It is important to understand what C is and what C++ is - since they're two entirely different languages!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if(((*p)->data%2)!=0 && *p)
    1st, consider the order of these two operations and what it is saying. You are asking the question "if the data pointed to by *p is even and *p is a valid pointer". Shouldn't you first ask if the pointer is valid and then ask the "is it even" question?

    2nd, consider that the only way to break out of the while(1) loop in the segregateOddAndEven function is by having *p be an invalid pointer. The next statement after the loop however attempts to access *p and assigns its "next" pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault!
    By coachortiz in forum C Programming
    Replies: 7
    Last Post: 09-14-2010, 09:18 AM
  2. Segmentation fault, how come?
    By Fader_Berg in forum C Programming
    Replies: 18
    Last Post: 08-31-2010, 01:58 AM
  3. Segmentation Fault hmm?
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 07:51 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM