std :: vector :: push_back会引发分段错误

时间:2012-01-06 16:02:06

标签: c++ vector segmentation-fault

我的程序有问题。对于少量边缘,它可以很好地工作,但是当它获得15000个定向图形边缘时,我会在运行一分钟后得到分段错误。调试器说它是由vector push_back方法抛出的。有人知道代码有什么问题以及如何避免代码吗?

在行 result.push_back(tmpResult)中的dfs过程中抛出错误;

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

typedef struct {
    unsigned int endNode;      // Number of dest node
    bool used;                 // true, if edge was used in dfs
} EdgeType;

typedef struct {
    unsigned int startNode;     // Number of source node
    vector<EdgeType> edge;      // Outgoing edges from node
} NodeType;

typedef struct {
    unsigned int startNode;
    unsigned int endNode;
} ResultType;


bool loadInput(vector<NodeType>& graph, unsigned int& numEdges);
void dfs(vector<NodeType>& graph, unsigned int i, unsigned int numEdges, vector<ResultType>& result);

int main(int argc, char** argv) {
    vector<NodeType> graph;
    vector<ResultType> result;
    unsigned int numEdges;

    result.reserve(300000);

    // Generate oriented multigraph (3 nodes, 150000 edges)
    numEdges = 150000;
    NodeType tmpNode;
    EdgeType tmpEdge;

    for (unsigned int i = 0; i < 50000; i++) {
        tmpEdge.used = false;
        tmpEdge.endNode = 1;
        tmpNode.edge.push_back(tmpEdge);     
    }
    tmpNode.startNode = 0;
    graph.push_back(tmpNode);
    tmpNode.edge.clear();

    for (unsigned int i = 0; i < 50000; i++) {
        tmpEdge.used = false;
        tmpEdge.endNode = 2;
        tmpNode.edge.push_back(tmpEdge);     
    }
    tmpNode.startNode = 1;
    graph.push_back(tmpNode);
    tmpNode.edge.clear();

    for (unsigned int i = 0; i < 50000; i++) {
        tmpEdge.used = false;
        tmpEdge.endNode = 0;
        tmpNode.edge.push_back(tmpEdge);     
    }
    tmpNode.startNode = 2;
    graph.push_back(tmpNode);
    tmpNode.edge.clear();

    cout << "numEdges: " << numEdges << endl;

    // Find way
    for (unsigned int i = 0; i < graph.size(); i++) {
        dfs(graph, i, numEdges, result);
    }

    // No way found
    cout << "-1" << endl;

    return 0;
}

void dfs(vector<NodeType>& graph, unsigned int i, unsigned int numEdges, vector<ResultType>& result) {
    // Way was found, print it and exit program (bad style, only for testing)
    if (numEdges == result.size()) {
        cout << graph.size() << endl;
        vector<ResultType>::iterator it;
        for (it = result.begin(); it != result.end(); it++) {
            cout << (*it).startNode << " " << (*it).endNode << endl;
        }
        cout << "0 0" << endl;
        exit(0);
    }
    // For each outgoing edge do recursion 
    for (unsigned int j = 0; j < graph[i].edge.size(); j++) {
        if (i >= graph.size()) return;
        if (!graph[i].edge[j].used) {
            graph[i].edge[j].used = true;
            ResultType tmpResult;
            tmpResult.startNode = graph[i].startNode;
            tmpResult.endNode = graph[i].edge[j].endNode;
            result.push_back(tmpResult);
            dfs(graph, graph[i].edge[j].endNode, numEdges, result);
            result.pop_back();
            graph[i].edge[j].used = false;
        }
    }
}

我的程序的目标是在面向图中找到一种方法,其中每个边只使用一次。

3 个答案:

答案 0 :(得分:6)

dfs以递归方式调用自身;增加numEdges会增加递归深度,因此,增加numEdges会导致堆栈溢出(在您的平台上显示为段错误)。

使用更大的堆栈大小(特定于编译器)构建程序,或者在此方案中不使用递归。

答案 1 :(得分:1)

最有可能的是你递归太深,导致堆栈溢出。在大多数平台上,堆栈具有固定的大小;你可能能够做得更大,但你可能仍然无法支持任意大的图形。

也许你可以用迭代算法替换递归,维护自己的堆栈(例如std::stack)你需要在回溯时恢复的状态。那么图表大小的唯一限制就是可用内存。

答案 2 :(得分:0)

如果push_back抛出,很可能是因为你的程序内存不足。由于您没有捕获任何异常,因此将调用默认异常处理程序并终止应用程序。在gdb中,您可以使用catch throw命令停止每个“throw”语句。