seg-faulting w/linked list but dont know why

This is a discussion on seg-faulting w/linked list but dont know why within the C++ Programming forums, part of the General Programming Boards category; For some reason when doing anything with my linked lists I am getting a seg-fault. All of my linked list ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    seg-faulting w/linked list but dont know why

    For some reason when doing anything with my linked lists I am getting a seg-fault. All of my linked list functions check to see if the head is NULL before adding or doing anything in the list to prevent seg-faults but for some reason that check in itself is causing the seg fault. Hopefully my code can clarify that...

    Code:
    void Laundry::addClean(Clothing c){
       if(c_head == NULL){
                 c_head = new node<Clothing>(c, NULL);
       }
       else
       {   
          c_tail->setLink(new node<Clothing>(c, NULL));
          c_tail = c_tail->link();
       }
       clean++;
       
    }
    that is where I am getting the seg-fault... and in any other function that uses if(some_head == NULL). in my last project I was having this same problem and it was because I had typed if(some_head = NULL). this time tho I have no idea what is wrong. My pointer is declared in laundry.h like this:

    Code:
    private: 
               node <Clothing> * c_head;
               node <Clothing> * c_tail;
    And my template node class:

    Code:
    template <class T>
    class node{
    
      public:
             node(){linkfield = NULL;}
             node(T d, node *lf){datafield = d; linkfield = lf;}
      
      T data(){return datafield;}
      node * link(){return linkfield;}
    
      void setData(T d){datafield = d;}
      void setLink(node *lf){linkfield = lf;}
       
     private: 
               T datafield;
                node *linkfield;
    };
    I've been at this for a while so maybe it's just something I'm not seeing and a fresh set of eyes will help. At any rate thanks for looking

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Your Laundry constructor(s) need to make sure c_head and c_tail are NULL
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 05:37 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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