Thread: linked list - crashes after inserting node to end of list

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    linked list - crashes after inserting node to end of list

    The program crashes at
    Code:
    (*temp).next = NULL;
    Code:
    #include <cstdlib>
    #include <iostream>
    //#include <cstring>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    LinkedList::LinkedList() : length(0), startPointer(NULL)
    {
    
    }
    
    LinkedList::~LinkedList()
    {
    
    }
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new Node;
            strcpy((*startPointer).name, name);//can't just assign with = because can't do arr1[]=arr2[]
            std::cout << "Your name is: " << (*startPointer).name <<  std::endl;
        }
        else
        {
            Node *temp = startPointer;
            while((*temp).next != NULL)
            {
                temp = (*temp).next;
                /*This isn't working*/
                (*temp).next = NULL;
            }
            (*temp).next = new Node;
            strcpy((*temp).name, name);
            std::cout << "Your last name is: " << (*startPointer).name <<std::endl;
        }
        return true;
    }
    Just for future referance, is it helpful if I leave in all the cout statment I use for debuging?
    Last edited by c_weed; 11-02-2010 at 11:42 AM. Reason: tidied up code

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    My crystal ball says that the default constructor for Node isn't setting the next pointer to NULL. This would cause your list to never be properly terminated, since you never do so explicitly in the createNode member function, and all loops that assume the list is NULL terminated will walk into oblivion. This often results in segmentation faults and crashes.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Quote Originally Posted by Prelude View Post
    My crystal ball says that the default constructor for Node isn't setting the next pointer to NULL. This would cause your list to never be properly terminated, since you never do so explicitly in the createNode member function, and all loops that assume the list is NULL terminated will walk into oblivion. This often results in segmentation faults and crashes.
    Thanks but that doesn't seem to be the problem.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    main.cpp
    Code:
    #include <iostream>
    //#include <cstring>
    #include "include\LinkedList.h"
    
    using namespace std;
    
    int main()
    {
        LinkedList myList = LinkedList();
        cout << "Enter your name: ";
        char myName[20];
        cin.getline(myName, 20, '\n');
        myList.createNode(myName);
        cout << "Enter your last name: ";
        char myLastName[20];
        cin.getline(myLastName, 20, '\n');
        myList.createNode(myLastName);
        return 0;
    }
    LinkedList.h
    Code:
    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    
    class LinkedList
    {
        public:
            LinkedList();
            ~LinkedList();
            bool createNode(char name[]);
    
        private:
            unsigned int length;
            struct Node
            {
                char name[20];
                Node *next;
            };
            Node *startPointer;
    
    };
    #endif
    LinkedList.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    //#include <cstring>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    //default constructor
    LinkedList::LinkedList() : length(0), startPointer(NULL){}
    
    //destructor
    LinkedList::~LinkedList(){}
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new Node;//Node is a struct
            strcpy((*startPointer).name, name);
            std::cout << "Your name is: " << std::endl;
        }
        else
        {
            Node *temp = startPointer;
            while((*temp).next != NULL)
            {
                temp = (*temp).next;
                /*This isn't working*/
                (*temp).next = NULL;
            }
            (*temp).next = new Node;
            strcpy((*temp).name, name);
        }
        return true;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_weed
    Thanks but that doesn't seem to be the problem.
    After looking at your code, I conclude that Prelude was correct. What made you conclude that Prelude's suggestion was off the mark?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Quote Originally Posted by laserlight View Post
    After looking at your code, I conclude that Prelude was correct. What made you conclude that Prelude's suggestion was off the mark?
    Prelude was right. I got the next pointer confused with the startPointer.
    So in my constructor I need
    Code:
    Node.next = NULL;
    but then I get "error: expected unqualified-id before '.' token"
    Last edited by c_weed; 11-03-2010 at 05:08 PM. Reason: changed everything

  8. #8
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    ah forget it, I'll start a new thread when I get my head wrapped around what question I should ask.
    Last edited by c_weed; 11-04-2010 at 05:58 PM. Reason: changed everything

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM