C++ Segmentation Fault When Calling Classes

This is a discussion on C++ Segmentation Fault When Calling Classes within the C++ Programming forums, part of the General Programming Boards category; The code below is where I have tried implementing a graph function. I have put cout statements in various places ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    C++ Segmentation Fault When Calling Classes

    The code below is where I have tried implementing a graph function. I have put cout statements in various places to pinpoint the error location of the segmentation fault. It goes through main, and into the graphType class. In the createGraph function it gets lost around the while loop (perhaps insertLast). Does anyone have an idea of where the error is occuring? Thank you in advance.

    MAIN:

    Code:
    #include <fstream>               //ifstream, ofstream functions
    #include <iostream>              //cin, cout functions
    #include "graphType.h"           //graphType object file    
    using namespace std;
    
    //Function prototypes
    void OpenInputFile (ifstream&, char[]);
    
    int main()
    {
       ifstream dataFileIn1;
       char inputFile1[30] = "graphSet1.dat";
     
    
       graphType graph;
    
       OpenInputFile(dataFileIn, inputFile1);
       graph.createGraph(dataFileIn);
       dataFileIn.close();
    
       return 0;
    }
    
    //function definitions................................................
    
    //Purpose:        Open an input file and test for successful open
    //Pre-condition:  File has been declared as input file stream, and
    //                file name is known
    //Post-condition: File is open if successful, otherwise terminate the program
    void OpenInputFile (ifstream& dataFileIn, char inputFile[])
    {
       //Open input file
       dataFileIn.open(inputFile);
    
       //If not successful open, display message and exit with error
       if (!dataFileIn)
       {
          cout << "\n\n\n\t\t\t Error: Input File Not Opened"
               << inputFile << endl;
          exit(1);
       }
    } //End OpenInputFile
    
     
    DATA FILE:

    Code:
    3
    0 1 2 -999
    1 2 -999
    2 -999
     
    GRAPH CLASS:
    Code:
    #ifndef H_graph
    #define H_graph
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include "linkedList.h"              //includes nodeType struct
    #include "linkedQueue.h"             //remove nodeType struct
    #include "unorderedLinkedList.h"
    
    using namespace std;
    
    class graphType
    {
    public:
        void createGraph(ifstream&);
          //Overloaded function to create a graph.
          //Postcondition: The graph is created using the  
          //    adjacency list representation.
    
        graphType(int size = 0); 
          //Constructor
          //Postcondition: gSize = 0; maxSize = size;
          //    graph is an array of pointers to linked lists.
    
        ~graphType();
          //Destructor
          //The storage occupied by the vertices is deallocated.
    
    protected:
        int maxSize;    //maximum number of vertices
        int gSize;      //current number of vertices
        unorderedLinkedList<int> *graph; //array to create 
                                         //adjacency lists 
    
    };
    
    void graphType::createGraph(ifstream& graphFile)
    {
        int vertex;
        int adjacentVertex;
    
        if (gSize != 0)     //if the graph is not empty, make it empty
            clearGraph();
    
        graphFile >> gSize; //get the number of vertices
    
        for (int index = 0; index < gSize; index++)
        {
            graphFile >> vertex;
            graphFile >> adjacentVertex;
    
            while (adjacentVertex != -999)
            {
                graph[vertex].insertLast(adjacentVertex);
                graphFile >> adjacentVertex;
            } //end while
        } // end for
    
    } //end createGraph
    
        //Constructor
    graphType::graphType(int size)
    {
        maxSize = size;
        gSize = 0;
        graph = new unorderedLinkedList<int>[size];
    }
    
        //Destructor
    graphType::~graphType()
    {
        clearGraph();
    }
    
    #endif
     
    LINKED LIST CLASS:
    Code:
    #ifndef H_LinkedListType
    #define H_LinkedListType
    
    #include <iostream>
    #include <cassert>
    
    using namespace std;
    
    //Definition of the node
    
    template <class Type>
    struct nodeType
    {
          Type info;
          nodeType<Type> *link;
    };
     
    UNORDERED LINKED LIST:

    Code:
    #ifndef H_UnorderedLinkedList
    #define H_UnorderedLinkedList
    
    #include "linkedList.h"
    
    using namespace std;
    
    template <class Type>
    class unorderedLinkedList: public linkedListType<Type>
    {
    protected:
       //Modification to recognize the inherited protected variables
       // from the base class when a template is used.
         using linkedListType<Type>::count;
         using linkedListType<Type>::first;
         using linkedListType<Type>::last;
    
    public:
        void insertLast(const Type& newItem);
          //Function to insert newItem at the end of the list.
          //Postcondition: first points to the new list, newItem is
          //    inserted at the end of the list, last points to the
          //    last node, and count is incremented by 1.
    };
    
    template <class Type>
    void unorderedLinkedList<Type>::insertLast(const Type& newItem)
    {
        nodeType<Type> *newNode; //pointer to create the new node
    
        newNode = new nodeType<Type>; //create the new node
    
        newNode->info = newItem;  //store the new item in the node
        newNode->link = NULL;     //set the link field of newNode
                                  //to NULL
    
        if (first == NULL)  //if the list is empty, newNode is 
                            //both the first and last node
        {
            first = newNode;
            last = newNode;
            count++;        //increment count
        }
        else    //the list is not empty, insert newNode after last
        {
            last->link = newNode; //insert newNode after last
            last = newNode; //make last point to the actual 
                            //last node in the list
            count++;        //increment count
        }
    }//end insertLast
    
    
    #endif
     

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    When you create the graphType in main, it is initialised with size zero (hence it's graph member is dynamically allocated with size 0).

    An array with size zero is one you cannot dereference (i.e. access elements of).

    The first value read from the file is 3. The loop you highlighted as a problem iterates from and accesses elements of the non-existent graph[0] to graph[3]. That is undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Awesome -- thanks, that did it.
    I worked for hours trying to fix other segments when it was just that one little error.
    (:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault Help
    By DuckCowMooQuack in forum C Programming
    Replies: 11
    Last Post: 02-08-2011, 07:03 PM
  2. Segmentation Fault Help
    By DuckCowMooQuack in forum C Programming
    Replies: 2
    Last Post: 02-08-2011, 01:16 AM
  3. Segmentation fault
    By browser in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 02:13 PM
  4. Segmentation Fault when Calling Functions
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 11:04 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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