0

I write some codes about finding strongly connected components.

using edge=pair<int, int>;

void dfsSCCFind(int vertex, vector<vector <int>>& adj, vector<int>& check, vector<int>& SCC, vector<edge>& SCCEdge){
    if(check[vertex]==0){
        check[vertex]=1;
        SCC.push_back(vertex);
        for(int i=0; i<(int)adj[vertex].size();i++){
            int neighbor=adj[vertex][i];
            if(check[neighbor]==0){
                SCCEdge.push_back({vertex, neighbor});
                dfsSCCFind(neighbor, adj, check, SCC, SCCEdge);
            }
        }
    }
}

My main function call 'dfsSCCFind'for all vertices in a graph.

But it makes unknown signal 11. I think it might be due to stackoverflow.

So, I added the code enlarge the stack size and it works properly.

However when I modify the code like below, it works properly without enlarging stack size.


void dfsSCCFind(int vertex, vector<vector <int>>& adj, vector<int>& check, vector<int>& SCC){
    if(check[vertex]==0){
        check[vertex]=1;
        SCC.push_back(vertex);
        for(int i=0; i<(int)adj[vertex].size();i++){
            int neighbor=adj[vertex][i];
            if(check[neighbor]==0){
                dfsSCCFind(neighbor, adj, check, SCC, SCCEdge);
            }
        }
    }
}

I just deleted a vector parameter and the code related with it.

I know vector is allocated on heap, not stack. Therefore, I think it is not related with "Unknown signal 11"

if it is related with the maximum heap size, it might not have been solved by enlarging stack size.

What is the cause of my code?

nimdraks
  • 13
  • 3
  • Signal 11 is a segmentation fault on Linux systems. Please refer to [this question](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) for some initial discussion and consider running your code under the watch of a debugger, ASAN, or equivalent to get better insight as to the cause. – nanofarad May 16 '20 at 03:58
  • Recursive functions like `dfsSCCFind` can easily blow off the end of the stack. Pop the code into the ol' debugger and use it to ensure that the exit conditions are behaving as expected. – user4581301 May 16 '20 at 04:05
  • @user4581301 Thank you for comments! I try to debug it at eclipse. But I still wander. I also found it works by using emplace_back, without deleting the code, or enlarging stack. – nimdraks May 17 '20 at 08:20
  • @nanofarad Thank you. I read the question and I try to debug it. – nimdraks May 17 '20 at 08:20

0 Answers0