Segmentation fault (What is that?)

#include <iostream>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    freopen("differ.inp","r",stdin);
    freopen("differ.out","w",stdout);
    int m,n;
    scanf("%d %d\n",&m,&n);
    char arr[m][n];
    for(int i = 0;i<m;i++){
        for (int j = 0;j<n;j++){
            scanf("%c",&arr[i][j]);
        }
        scanf("\n");
    }
    char arr1[m][n];
    for(int i = 0;i<m;i++){
        for (int j = 0;j<n;j++){
            scanf("%c",&arr1[i][j]);
        }
        scanf("\n");
    }
    int counter = 0;
    for (int i = 0;i<m;i++){
        for (int j = 0;j<n;j++){
            if (arr[i][j]!=arr1[i][j]) counter++;
        }
    }
    printf("%d",counter);
    return 0;
}

What is the error here? Every time I try out with the char, it will show Segmentation (now it also has bus error). What is the problem here?

Typically, a segmentation fault means that you are accessing a memory location that you should not be accessing.

It is hard to say much without your input files or a stack trace of the error.

I’d recommend using Valgrind to get a full stack trace of the error. You will get an exact line number where your issue appears.

That is absolutely the easiest way to debug memory issues with C and C++ code.

I don’t know.
I used Valgrind, and it said that no memory leaking.
And, the problem is getting me headache again :frowning:.

What was the stack track of the segmentation fault from Valgrind?

Actually, I found out the problem.
Sorry for inconvenience, I am just a beginner in C++ :sweat_smile: :sweat_smile: :sweat_smile:
Thanks for suggestion

1 Like

Valgrind is good for detecting certain memory errors, but to do any serious dev work in C or C++, you’ll want to get to know your debugger. Your IDE should have support built-in, and if not, get an IDE that does. With the IDE support, you just click the “debug” button instead of “run” and it Should Just Work.