linked list - crashes after inserting node to end of list

This is a discussion on linked list - crashes after inserting node to end of list within the C++ Programming forums, part of the General Programming Boards category; The program crashes at Code: (*temp).next = NULL; Code: #include <cstdlib> #include <iostream> //#include <cstring> #include <string.h> #include "..\include\LinkedList.h" LinkedList::LinkedList() ...

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

    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,796
    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
    116
    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
    21,594
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    116
    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
    21,594
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    116
    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
    116
    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, 03:09 AM
  3. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  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