创建类点后的 C++ 分段错误
c++ segmentation fault after creating a point of a class
我有一个抛出分段错误的 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;
}
更安全,但将责任放在接收器身上,以确保在尝试使用它之前它收到的内容是有效的。
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)