【问题标题】:How to clear this push_back() segmentation fault?如何清除此 push_back() 分段错误?
【发布时间】:2014-11-18 21:54:19
【问题描述】:

我有一个 * 类型的向量和一个 Vertex* 类型的变量 k。当我尝试在这个向量 v 中推回 k 时,有一个段错误(核心转储)。知道是什么原因造成的吗?这是代码,如果有帮助的话。

我的代码基本上是一个 BFS 图搜索,用于查找 2 个顶点之间的最短路径。

queue<Vertex*> myqueue;
unordered_map<Vertex*,Vertex*> mymap;
std::vector<Vertex*>* v;
//std::vector<Vertex*>* d;
Vertex* k;
Vertex* f;
Vertex* l;
//here I will store the visited vertices(ones that've been put into the queue.) as well as the the previous nodes of this unordered map.

if(!containsVertex(v1)||!containsVertex(v2))
{
    cout<<"The vertices you wish to find the shortest path between, don't exist. You might want to check them."<<endl;
    return NULL;
}

myqueue.push(v1);
mymap[v1]=NULL;
cout<<"Pushed v1"<<endl;
while(!myqueue.empty())
{
    cout<<"Entered while loop"<<endl;
    f=myqueue.front();
    myqueue.pop();

    cout<<"executed popping"<<endl;
    for(int i=0;i<neighbors(f).size();i++)
    {
        cout<<"entered for loop"<<endl;
        if(neighbors(f).at(i)==v2)
        {
            cout<<"found the guy. Preparing to output the vector"<<endl;
            k=v2;
            mymap[v2]=f;
            cout<<"Is the fault here?"<<endl;
            while(mymap[k]!=v1)
            {
                cout<<"Entered the while loop"<<endl;
                v->push_back(k);
                cout<<"After the pushback?"<<endl;
                cout<<v->size()<<endl;
                cout<<"After outputting the size?"<<endl;
                l=mymap[k];
                cout<<"After the mapping retrieval?"<<endl;
                k=l;
                cout<<"after the end assignment?"<<endl;
            }
            cout<<"Exited the while loop"<<endl;
            v->push_back(v1);
            return v;
        }
        if(mymap.find(neighbors(f).at(i))==mymap.end())
        {
            myqueue.push(neighbors(f).at(i));
            mymap[neighbors(f).at(i)]=f;
        }
    }
}
cout<<"There doesn't exist a shortest path between these two vertices."<<endl;
return NULL;

段错误出现在我尝试开始推动向量中的元素的部分。

【问题讨论】:

  • 所以,您只想清除指标,而不是修复错误?无论如何,需要一个MCVE

标签: c++ vector segmentation-fault breadth-first-search


【解决方案1】:

在您的代码中,您有:

std::vector<Vertex*>* v;
// bunch of lines that don't reference v
v->push_back(k);

v 绝不会指向实际对象。你需要一个实际的向量来 push_back 。要么:

std::vector<Vertex*>* v = new std::vector<Vertex*>;
// remember to delete it

或者,你真的需要指针吗?

std::vector<Vertex*> v;
// bunch of lines
v.push_back(k);

【讨论】:

    【解决方案2】:

    您永远不会分配v,它是指向堆上一个向量(指针...)的指针。这就是你出现段错误的原因。

    在使用 v 之前,添加类似的内容可以解决您当前的问题:

    v = new std::vector<Vertex*>;
    

    除了这个错误之外,您可能还需要考虑遍历您的设计并确定这种类型的数据结构是否有意义。正如其他人所指出的,MCVE 示例可以让我们更好地了解您想要做什么。

    也就是说,定义一个指向包含原始指针的向量的原始指针可能不是您想要做的。无需仔细研究您的代码,将 v 更改为本地范围(在您的堆栈上)向量可能就可以完成任务

    std::vector<Vertex*> v;
    

    (然后将所有v-&gt;更改为v.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2022-11-21
      • 2020-08-22
      相关资源
      最近更新 更多