Doubly Linked List Segmentation Fault

This is a discussion on Doubly Linked List Segmentation Fault within the C Programming forums, part of the General Programming Boards category; So i've never really worked with doubly linked lists in terms of coding, and I didn't do great with singly ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    42

    Doubly Linked List Segmentation Fault

    So i've never really worked with doubly linked lists in terms of coding, and I didn't do great with singly linked lists either. I have it all coded out but it's giving me a segmentation fault when I run it. I have looked over all my code and have no idea whats wrong, in add event i malloc one node of space then find where to add it and add it. i only add 1 per call to add event so that shouldnt be an issue. is there an initial part im missing when you first start the list?

    [note all of the variables and definitions you see that aren't in the code are in one of the 2 external files if u need more info from them to help let me know.


    osdefs.h
    Code:
    struct event_list
    {
    	/** Type of event. */
    	event_type          event;
    
    	/** User or device causing the event. */
    	int                 agent;
    
    	/** Time event occurs. */
    	struct time_type    time;
    
    	/** Next event node in list. */
    	struct event_list*  prev;
    
    	/** Previous event node in list. */
    	struct event_list*  next;
    };
    typedef struct event_list event_list;
    externs.h
    Code:
    extern event_list*    Event_List;  ///< Pointer to head of event list
    obj1.c
    Code:
    void Add_Event( int event, int agent, struct time_type* time )
    {
                   //Header points to the head of the list.
                  
                  event_list* header = Event_List;
                  
                  
                  
                  event_list* temp;
                  temp = malloc(sizeof(event_list));
                  temp->event = event;
                  temp->agent = agent;
                  temp->time.nanosec = time->nanosec;
                  temp->time.seconds = time->seconds;
                  temp->next = NULL;
                  temp->prev = NULL;
                  
                  if(Event_List==NULL)
                  {
                        // header = temp;
                         Event_List = temp;
                         return;
                             
                  }
                  //While header is not the last node in the list.
                  while(header!=NULL){
                      //First compare the temporary nodes seconds to the header's seconds. If they are the same:
                      //compare nanoseconds of each node, if we enter the loop then temp precedes header.
                      if(temp->time.seconds < header->time.seconds || (temp->time.seconds == header->time.seconds && temp->time.nanosec <= header->time.nanosec))                        
                       {       
                               //Make temp the new head of the list.
                               //temp->next now pointts to header.          
                               if(header->prev = NULL)
                               {
                                  header->prev = temp;
                                  temp->next = header;
                                  temp->prev = NULL;  
                                  Event_List = temp; 
                                  header = NULL;         
                               
                               }
                               
                               else
                               {                                                                
                                  //Insert into the middle of the list.
                                  
                                    header->prev->next = temp;
                                    temp->prev = header->prev;
                                    header->prev = temp;
                                    temp->next = header;
                                    header = NULL;         
                                   
                               }             
                                                              
                      
                      }//end outer if
                      
                //Inserting at the end of the list temp->previous is set as header.      
               else if(header->next == NULL)
               {
                 header->next = temp;
                 temp->prev = header;
                 temp->next = NULL;
                 header = NULL;
                    
               }  
               //Advance the header to the next node in the list.
               else
                   header=header->next;
               }//end while
             
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One little tip that may help you sort this out... you have two very similar names going on here... Event_List and event_list ... the first refers to your entire list the second refers to one event in the list... Rename the single event Event_Item throughout your code... then look at your code again.

    Hint: One of the very best debugging tools you can have are the names of your variables.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    I can't, that stuff in the headers was given to me and im not allowed to alter it.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    through commenting out sections ive narrowed the segmentation fault down to this part:

    Code:
      if(header->prev == NULL)
                               {
                                  header->prev = temp;
                                  temp->next = header;
                                  temp->prev = NULL;  
                                  Event_List = temp; 
                                  header = NULL;         
                               
                               }
                             else
                               {                                                                
                                  //Insert into the middle of the list.
                                  
                                    header->prev->next = temp;
                                    temp->prev = header->prev;
                                    header->prev = temp;
                                    temp->next = header;
                                    header = NULL;         
                                   
                               }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    if(header->prev = NULL)
    = vs ==

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    yup. almost got the output right just a few more tweaks thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubly linked list
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 07-21-2009, 10:32 AM
  2. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 04:41 AM
  3. doubly linked list
    By bahareh in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2007, 02:31 PM
  4. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 02:39 PM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21