I have a c++ code segment that throws segmentation Error:
Matrix representation of graph
0100
0010
1011
0000
Node, label:1,column index:0
/C/Program Files/NetBeans 8.1/ide/bin/nativeexecution/dorun.sh: line 33: 13140 Segmentation fault sh "${SHFILE}"
code snippet:
void graph::shortest_path(std::vector<std::vector<int>> &_matrix) {
//1) initialize not visited nodes
int num_of_nodes = _matrix.size();
std::queue<Node> unvisited;
std::queue<Node> visited;
for (int i = 0; i < num_of_nodes; i++) {
unvisited.push(Node(i + 1));
}
//while unvisited is NOT empty
while (!unvisited.empty()) {
//2) pop/remove from unvisited & are there adjacent neighbors
Node node = unvisited.front();
std::cout << "Node, label:" << node.getLabel() << ",column index:" << node.getLabel() - 1 << endl;
vector<int>& columnVector = _matrix[node.getLabel() - 1];
//nodes integer label
int label = 0;
//loop add adjacency list * back pointers
for (int c = 0; c < columnVector.size(); c++) {
//if there are actual connections, then adjacency matrix value at C NOT equal INT_MAX
if (columnVector[c] != INT_MAX) {
//create a node & add to current nodes adjacency list
Node * adj = new Node(c+1);
adj->setPrev(node);
node.addToAdjacenyList(*adj);
//also set the prev reference
}
}//end loop add adjacency list * back pointers
//3) for each node calculate the total weight or cost back to the start & select the min
for (int i = 0; i < node.getAdjacenyList()->size(); i++) {
cout << node.getAdjacenyList()->at(i).getLabel() << ",";
}
cout << endl;
//keep track of visited nodes
visited.push(node);
//since this node was visited remove/pop it from unvisited
unvisited.pop();
}
}
However when I just instantiate without the new keyword I dont get any segmentation fault:
//loop add adjacency list * back pointers
for (int c =0; c<columnVector.size(); c++)
{
//if there are actual connections, then adjacency matrix value at C NOT equal INT_MAX
if (columnVector[c]!=INT_MAX)
{
//create a node & add to current nodes adjacency list
//Node * adj = new Node(c+1);
//adj->setPrev(node);
node.addToAdjacenyList(Node(c+1));
//also set the prev reference
}
}//end loop add adjacency list * back pointers
Ok Output:
Matrix representation of graph
0100
0010
1011
0000
Node, label:1,column index:0 2, Node, label:2,column index:1 3, Node, label:3,column index:2 1,3,4, Node, label:4,column index:3
**Here is the Node class:**
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "Node.h"
#include <vector>
Node::Node()
{
}
Node::Node(int label):_label(label)
{
}
Node::~Node()
{
Node::_adjacency_list->clear();
}
void Node::setLabel(int val)
{
_label = val;
}
int Node::getLabel()
{
return _label;
}
void Node::setWeight(int val)
{
_weight = val;
}
int Node::getWeight()
{
return _weight;
}
int Node::getDistance()
{
return _distance;
}
void Node::setDistance(int val)
{
_distance = val;
}
/*
* std::vector<Node>* getAdjacenyList();
void addToAdjacenyList(const Node& node);
*/
std::vector<Node>* Node::getAdjacenyList()
{
std::vector<Node>* point = &*_adjacency_list;
return point;
}
void Node::addToAdjacenyList(const Node& node)
{
_adjacency_list->push_back(node);
/*
*
* Node* getPrev();
void setPrev(const Node& node);
*/
}
Node* Node::getPrev()
{
Node* point = &*_prev;
return point;
}
void Node::setPrev(const Node& n)
{
*_prev = n;
}
Why am I getting segmentation fault & how can I fix it?
Thanks