Thread: seg-faulting w/linked list but dont know why

  1. #1
    Registered User
    Join Date
    Apr 2005

    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...

    void Laundry::addClean(Clothing c){
       if(c_head == NULL){
                 c_head = new node<Clothing>(c, NULL);
          c_tail->setLink(new node<Clothing>(c, NULL));
          c_tail = c_tail->link();
    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:

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

    template <class T>
    class node{
             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;}
               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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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, 06:37 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM