Thread: Linked List, node creation question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Linked List, node creation question

    Hey guys,
    I'm getting a seg fault error when I run my program (the error comes with the assignNodes function), i'm realizing at this point im doing something wrong with making a new node to put the info in, but i'm still stuck on what to do. Do I need to write another line to create new nodes or make a different pointer? Here are my files. What i'm trying to do is read from a file and make an array of the info, then put that array into the nodes. Then repeat the process for every person (this is an ordered link list address book). Thanks!
    Thanks!

    Code:
    /*
     *  addressbook.cpp
     *  Homework2
     *
     *  Created by Alec on 10/2/10.
     *
     */
    
    using namespace std;
    
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include "SinglyLinkedList.h"
    //String streams are similar to the <iostream> and <fstream> libraries,
    // except that string streams allow you to perform I/O on strings instead of streams. 
    string upperCase(string lowerCase);
    void assignNodes(string info[]);
    
    
    
    int main()
    {
    	int i;
    	ifstream fin;
    	string word; 
    	string line;
    	string strInfo[10];
    
    	
    	fin.open("address.dat");
    	while (getline(fin,line))
        {
    		int i = 0;
    		cout << "Contact: " << line << endl;
    		stringstream stream(line);
    		while( getline(stream, word, ',') )
    		{
    			
    			cout << upperCase(word) << endl;
    			strInfo[i] = upperCase(word);
    			i++;
    			
            }
    		
    		 assignNodes(strInfo);
    		
    		
        }
    	fin.close();
    	
    	return 0;
    	
    }
    
    
    
    	string upperCase(string lowerCase)
    {
    		int i=0;
    		
    		for( i = 0; i < lowerCase.length(); i++)
    		{
    			lowerCase[i] = char(toupper(lowerCase[i]));
    			
    		}
    		
    		return lowerCase;
    		
    }
    
    	void assignNodes(string info[])
    {
    	contact *person;
    	person -> lastName = info[0];
    	person -> firstName = info[1];
    	person -> address1 = info[2];
    	person -> address2 = info[3];
    	person -> city = info[4];
    	person -> state = info[5];
    	person -> zipCode = info[6];
    	person -> phoneNum = info[7];
    	person -> dob = info[8];
    	person -> relation = info[9];
    	
    }
    Code:
    /*
     *  SinglyLinkedList.h
     *  Homework2
     *
     *  Created by Alec on 10/2/10.
     *
     */
    
    # include <string>
    
    using namespace std;
    
    struct contact {
    	
    	contact *head;
    	contact *first;
    	contact *last;
    	contact *next;
    	contact *back;
    	string lastName;
    	string firstName;
    	string address1;
    	string address2;
    	string city;
    	string state;
    	string zipCode;
    	string phoneNum;
    	string dob;
    	string relation;
    	
    	
    };
    Last edited by Alec0905; 10-02-2010 at 07:57 PM.

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    You never even allocate the space for the pointer "person"

    Code:
    	void assignNodes(string info[])
    {
    	contact *person;
    	person -> lastName = info[0];
    	person -> firstName = info[1];
    	person -> address1 = info[2];
    	person -> address2 = info[3];
    	person -> city = info[4];
    	person -> state = info[5];
    	person -> zipCode = info[6];
    	person -> phoneNum = info[7];
    	person -> dob = info[8];
    	person -> relation = info[9];
    	
    }
    should be :

    Code:
    contact *person = new contact
    you will want to do:

    Code:
    delete person
    later on when you want to remove the node. Also, you have this function return type as void. You wont be able to access this pointer when the function exits. It only has a local scope to this function. Either return it, or pass a contact pointer as an argument and allocate that way.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Syscal View Post
    Code:
    contact *person = new contact
    you will want to do:

    Code:
    delete person
    I suggest:
    Code:
    auto person = std::make_shared<contact>();
    Alternatively,
    Code:
    std::tr1::shared_ptr<contact> person(new contact);
    later on when you want to remove the node. Also, you have this function return type as void. You wont be able to access this pointer when the function exits. It only has a local scope to this function. Either return it, or pass a contact pointer as an argument and allocate that way.
    Returning it is probably the best idea, since shared_ptrs only have an explicit constructor and no assignment operator for T*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Time for me to learn something new. What is the benefit of using a shared_ptr over a plain ol' pointer? I'm not even sure what a shared_ptr is. I will have to look that up.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Syscal View Post
    Time for me to learn something new. What is the benefit of using a shared_ptr over a plain ol' pointer? I'm not even sure what a shared_ptr is. I will have to look that up.
    shared_ptr is mentioned so many times, that you must have heard about it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Syscal View Post
    Time for me to learn something new. What is the benefit of using a shared_ptr over a plain ol' pointer? I'm not even sure what a shared_ptr is. I will have to look that up.
    It frees its associated pointer when all shared_ptrs pointing to that same pointer has gone out of scope. Hence it's called a "smart pointer."
    It frees the programmer of the burden of calling delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  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. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM