创建一个类之后的c ++分段错误

时间:2018-03-23 18:11:40

标签: c++ segmentation-fault

我有一个c ++代码段,会引发细分错误:

图表的矩阵表示

0100

0010

1011

0000

节点,标签:1,列索引:0

/ C / Program Files / NetBeans 8.1 / ide / bin / nativeexecution / dorun.sh:line 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();


    }

}

然而,当我只是在没有new关键字的情况下实例化时,我没有遇到任何分段错误:

//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,列索引:0 2, 节点,标签:2,列索引:1 3, 节点,标签:3,列索引:2 1,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; 
}

为什么我会出现分段错误&amp;我该如何解决?

由于

1 个答案:

答案 0 :(得分:1)

void Node::setPrev(const Node& n)
{
    *_prev = n; 
}

_prev永远不会指向dereferencing it之前的有效内存位置并将n复制到其中。取消引用无效指针会调用可怕的Undefined Behaviour。复制到无效内存也是未定义行为,很可能是导致分段错误的原因。

可能你想要的是

void Node::setPrev(Node * n)
{
    _prev = n; 
}

但这可能会在其他地方引发一系列可能令人讨厌的事件,因为邻接名单也存储副本。需要进行重大的重新设计。

中有立即回声
Node* Node::getPrev()
{
     Node* point = &*_prev;
     return point; 
}

&*_prev;取消引用指针并立即再次获取地址。如果_prev指向有效内存,则净结果无效。如果_prev未指向有效内存,而不是{1}},则*_prev的结果未定义。

Node* Node::getPrev()
{
     return _prev; 
}

更安全,但将责任放在接收器上,以确保在尝试使用它之前收到的内容是有效的。