C++ Segmentation fault Exception Question.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

The first code is as follows:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void dfs(int x, int m, bool *visited, vector<vector<int>> &g){
    visited[x] = true;
    cout << x << endl;

    cout << "plz" << endl;

    For (inti=0; i<m; i++) { element output of //vector
        for (int j=0; j<2; j++){
            cout << g[i][j] << '\n';
        }
    }
}

int main(){
    freopen("input.txt", "r", stdin);

    int n, m, v;
    cin >> n >> m >> v;

    vector<vector<int>> g(m, vector<int>(2,0));
    for (int i=0; i<m; i++){
        int t1, t2;
        cin >> t1 >> t2;
        g[i][0] = t1;
        g[i][1] = t2;
    }
    sort(g.begin(), g.end());

    bool visited(n+1);

    dfs(1, m, &visited, g);

    return 0;
}

The code in question is

in the dfs function
 for (inti=0; i<m; i++) element output of { //vectorg
        for (int j=0; j<2; j++){
            cout << g[i][j] << endl;

This is the double for statement.

위 코드 전체를 제 컴퓨터 VScode에서 mingw의 g++ 컴파일러로 컴파일 한 뒤 실행하면 < img alt = "image" = src cloudinary. // res. : " https com image upload to / / eightcruz v 1651861293 hbnxnaypmwx dtlmpgbz 9.png"/>

As shown above, only plz is output, and the double for statement in the dfs function is not executed.

When I tried debugging, an exception called segmentation fault occurred.

Even if you run it in dev-c++, it only outputs up to plz.

But what's ridiculous is that if you run it on an online idea like repl.it,

The results come out well like in the picture above.

Why do errors only occur on my computer?

And the weird thing is, if you don't call the dfs function and run the double for statement in question, you can move it to the main function and run it well.

If my coding is wrong, I think the same error should occur on repl.it, but I don't know what's wrong with it.

I reset my laptop because of this, but it's the same.

The reason I think is that something went wrong when passing vectorg to the dfs function as a parameter

Compilers on sites like repl.it have translated well on their own for some reason, but my g++ and dev-c++ compilers don't.

What do you think? <

c++ segmentation-fault

2022-09-20 10:29

1 Answers

bool visited(n+1);

The code above is invalid.

I think what I originally wanted to do was declare a bowl-type array, visited, but to declare an array, I have to do it in square brackets ([][]) instead of square brackets (()).

bool visited[n + 1];

However, since you cannot declare an array with variables, if you describe it as above, it will be an error, and you must declare an array with a dynamic assignment.

bool* visited = new bool[n + 1];
memset(visited, 0, n + 1);

Once you change it like above and run it, the segmentation fault will be resolved.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void dfs(int x, int m, bool* visited, vector<vector<int>>& g) {
    visited[x] = true;
    cout << x << endl;

    cout << "plz" << endl;

    For (inti = 0; i < m; i++) { //vector output
        for (int j = 0; j < 2; j++) {
            cout << g[i][j] << '\n';
        }
    }
}

int main() {
    //freopen("input.txt", "r", stdin);

    int n, m, v;
    cin >> n >> m >> v;

    vector<vector<int>> g(m, vector<int>(2, 0));
    for (int i = 0; i < m; i++) {
        int t1, t2;
        cin >> t1 >> t2;
        g[i][0] = t1;
        g[i][1] = t2;
    }
    sort(g.begin(), g.end());

    bool* visited = new bool[n + 1];
    memset(visited, 0, n + 1);

    //bool visited[n + 1];
    //vector<bool> visited(n + 1);

    dfs(1, m, visited, g);

    return 0;
}

vector<bool> visited(n + 1);

However, rather than declaring a bowl-type array, it is declared as a vector as above It is more convenient to change the third parameter of the function as shown below.

void dfs(int x, int m, vector<bool>& visited, vector<vector<int>>& g) {


2022-09-20 10:29

If you have any answers or tips


© 2022 pinfo. All rights reserved.