cyber101
cyber101

Reputation: 2994

c++ segmentation fault trying to access vector

Im trying to create a adjacency representation of a graph.

I wrote a small program using vectors of vectors , however I keep getting "segmentation fault" but the compiler(clang++ version 5.0.1 on Windows) it seems wereever I try to access the vector vertex_matrix its giving a segmentation fault, why is it not being instantiated?

Here is the header:

#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality 
class graph
{
    public:
    graph(int);
    ~graph();
    void add_edge(int v1, int v2, int weight); 
    void print_graph();

    private:
    std::vector<std::vector<int>> vertex_matrix; 
    int num_of_vertices;
    int num_of_edges; 


};
#endif

Here is the cpp implementation:

#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality 

graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices)
    {

        if (_num_of_vertices==0)
        {
            _num_of_vertices=10;
        }

        for (int i = 0; i < _num_of_vertices; i++)
        {
             vertex_matrix[i]=(vector<int> (_num_of_vertices,INT_MAX));
        }


    }

    graph::~graph()
    {
    vertex_matrix.clear(); 
    }

    void graph::add_edge(int v1, int v2, int weight)
    {
        //vertex_matrix[v1-1][v2-1] == INT_MAX

        vector<int> columnVector = vertex_matrix[v1-1];
        if (columnVector[v2-1] == INT_MAX)
            {
                columnVector[v2-1] = weight;
            }
    }

void graph::print_graph()
{
    cout << "vertex_matrix size:" << vertex_matrix.size() << endl;
    for (int i=0; i< num_of_vertices; i++)
    {

        for (int j = 0; j < num_of_vertices; j++)
            {
                //vertex_matrix[i][j]
                std::vector<int> columnVector = vertex_matrix[i]; 

                if (columnVector[j] != INT_MAX)
                {
                    std::cout << columnVector[j] ;  
                }
                else
                {
                        std::cout << "0";
                }

            }

            std::cout << endl; 
    }//end for printing 


}

Here is the main entry:

#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;

    graph _graph(4);
    _graph.add_edge(1,2,1);
    _graph.add_edge(2,3,1);
    _graph.add_edge(3,1,1);
    _graph.add_edge(3,3,1);
    _graph.add_edge(3,4,1);
    _graph.add_edge(4,0,0);

    _graph.print_graph(); 


}

I edited the above code to use pass by reference, however the matrix still prints as 0's.

Please help with pass by reference, updates below:

Header:

#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality 
class graph
{
    public:
    graph(int);
    ~graph();
    void add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & matrix); 
    void print_graph();
    std::vector<std::vector<int>> vertex_matrix; 


    private:
    int num_of_vertices;
    int num_of_edges; 


};
#endif

Cpp file:

#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality 

graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices) {

    if (num_of_vertices == 0) {
        num_of_vertices = 10;
    }

    for (int i = 0; i < num_of_vertices; i++) {

         std::vector<std::vector<int>>& matrix = vertex_matrix; 
         matrix.push_back(vector<int> (num_of_vertices, INT_MAX));
    }

}

graph::~graph() {
    std::vector<std::vector<int>>& matrix = vertex_matrix; 
    matrix.clear();
}

void graph::add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & _matrix) {

    //vertex_matrix[v1-1][v2-1] == INT_MAX 
    vector<int> columnVector = _matrix[v1 - 1];
    if (columnVector[v2 - 1] == INT_MAX) {
        columnVector[v2 - 1] = weight;

    }


}


void graph::print_graph() {

     std::vector<std::vector<int>>& matrix = vertex_matrix; 

    for (int i = 0; i < matrix.size(); i++) {

        for (int j = 0; j < matrix.size(); j++) {
            //vertex_matrix[i][j]
            std::vector<int> columnVector = matrix[i];

            if (columnVector[j] != INT_MAX) {
                std::cout << columnVector[j];
            } else {
                std::cout << "0";
            }


        }

        std::cout << endl;
    }//end for printing 


}

main:

#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;

    graph _graph(4);
             std::vector<std::vector<int>>& m =  _graph.vertex_matrix;
    _graph.add_edge(1,2,1,m);
    _graph.add_edge(2,3,1,m);
    _graph.add_edge(3,1,1,m);
    _graph.add_edge(3,3,1,m);
    _graph.add_edge(3,4,1,m);
    _graph.add_edge(4,0,0,m);

    _graph.print_graph(); 



}

Any help will be appreciated.

Thanks

Upvotes: 0

Views: 997

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122458

You create an empty vector and then try to access elements in it. Change your constructor to

graph::graph(size_t _num_of_vertices) : 
    vertex_matrix(
        std::vector<std::vector<int>>(
            _num_of_vertices,std::vector<int>(_num_of_vertices)
        )
    )       
{}

to create a correctly sized vector.

Also in case _num_vertices == 0 you set it to 10 but thats after you initialized the member num_vertices so you leave the object in an inconsistent state. There are different ways to fix that, I would probably just throw an exception when the number of vertices passed is zero, or just ignore it. User wants a zero sized matrix? Why not?

Moreover the size should be unsigned not signed, there is size_t for container sizes. Even better you shouldnt have that member at all, because a vector already knows its size, the only reason to repeat that information is to introduce mistakes ;)

Upvotes: 1

Related Questions