我有一个抛出分段错误的 c++ 代码段:
图的矩阵表示
0100
0010
1011
年0000
节点,标签:1,列索引:0
/C/Program Files/NetBeans 8.1/ide/bin/nativeexecution/dorun.sh:第 33 行:13140 分段错误 sh "${SHFILE}">
代码片段:
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();
}
}
但是,当我只是在没有新关键字的情况下实例化时,我没有得到任何细分错误:
//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
正常输出:
图的矩阵表示
0100
0010
1011
年0000
节点,标签:1,列索引:02,节点,标签:2,列索引:13,节点,标签:3,列索引:21,3,4,节点,标签:4,列索引: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;
}
为什么我会出现分段错误以及如何解决它?
谢谢
void Node::setPrev(const Node& n)
{
*_prev = n;
}
_prev
在取消引用n
并将复制到其中之前,永远不会指向有效的内存位置。取消引用无效指针会调用可怕的未定义行为。复制到无效内存也是未定义的行为,并且可能是导致分段错误的原因。
可能你需要的意图是
void Node::setPrev(Node * n)
{
_prev = n;
}
但这可能会在其他地方引发一连串令人讨厌的事件,因为邻接列表也在存储副本。重大的重新设计正在进行中。
有立即的回声
Node* Node::getPrev()
{
Node* point = &*_prev;
return point;
}
&*_prev;
取消引用指针并立即再次获取地址。如果_prev
指向有效内存,则最终结果是无用的。如果_prev
未指向有效内存,并且它不是,则*_prev
的结果是未定义的。
Node* Node::getPrev()
{
return _prev;
}
更安全,但将责任放在接收器身上,以确保在尝试使用它之前它收到的内容是有效的。