【问题标题】:c++ segmentation fault trying to access vector尝试访问向量的c ++分段错误
【发布时间】:2018-08-30 04:50:22
【问题描述】:

我正在尝试创建图形的邻接表示。

我使用向量的向量编写了一个小程序,但是我不断收到“分段错误”但编译器(Windows 上的 clang++ 版本 5.0.1)似乎每当我尝试访问向量 vertex_matrix 它给出了分段错误,为什么没有被实例化?

这是标题:

#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

这里是 cpp 实现:

#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 


}

这里是主要条目:

#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(); 


}

我编辑了上面的代码以使用按引用传递,但是矩阵仍然打印为 0。

请帮忙通过引用,更新如下:

标题:

#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 文件:

#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 


}

主要:

#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(); 



}

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 在构造函数中,当 vertex_matrix 为空时,您将其分配给它的元素。这意味着 每个 索引都超出范围,您将有 未定义的行为
  • add_edge中有未定义的行为,你使用超出范围的索引(你传递例如0作为参数@987654331 @,这将引导您使用索引-1)。
  • 构造函数应该使用成员变量num_of_vertices,而不是参数_num_of_vertices ,并且add_edge不会修改矩阵成员。 (C++ 不是 Java。准备忘掉很多东西。)
  • @molbdnilo,是的,你是对的,我必须使用按引用传递而不是按值传递,因为当 add_edge 使用按值传递访问向量时,会生成向量的内部副本函数/内存堆栈及其丢失。当我通过参考 add_edge(int v1, int v2, int weight, vectot>& matrix) 时,我指向 matrix_vector 而不是临时副本。
  • @cyber101 问题是您正在修改add_edge 中每一列的副本。您更新的代码完全没有使用对象的要点 - 您的第一个界面还可以。

标签: c++


【解决方案1】:

您创建一个空向量,然后尝试访问其中的元素。将您的构造函数更改为

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

创建一个正确大小的向量。

如果_num_vertices == 0 将其设置为10,但那是在您初始化成员num_vertices 之后,因此您使对象处于不一致状态。有不同的方法可以解决这个问题,当传递的顶点数为零时,我可能会抛出异常,或者只是忽略它。用户想要一个零大小的矩阵?为什么不?

此外,尺寸应为unsigned 未签名,容器尺寸为size_t。更好的是,您根本不应该拥有该成员,因为 vector 已经知道它的大小,重复该信息的唯一原因是引入错误;)

【讨论】:

  • user463035818,您为我指明了正确的方向。但是,正如你提到的,有几种方法可以解决这个问题,我做到了: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 &lt; _num_of_vertices; i++) { vertex_matrix.push_back(vector&lt;int&gt; (_num_of_vertices, INT_MAX)); } }
  • @cyber101 这是一种方法,虽然效率很低。您应该尽可能选择初始化列表,调用 push_back 会导致多次重新分配,并可能导致向量多次复制其数据,在您的情况下可以轻松避免
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多