I get a segmentation fault in the following C++ program.
#include<iostream>
const int N = 3000;
int main() {
bool coprimes[N][N];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
coprimes[i][j] = ((3 * i + j) % 17 == 0);
}
}
for (int c = 1; c < N; ++c) {
for (int a = 1; a < c / 2; ++a) {
int b = c - a;
if (!coprimes[a][b] || !coprimes[a][c] || !coprimes[b][c]) {
continue;
}
std::cout << c << std::endl;
}
}
}
(The program as is does not make much sense. I just tried to create somewhat of a minimum working example.)
The weird thing is, it's at the line std::cout << c << std::endl;. If I remove that line, the program works fine.
Can anyone explain to me why this happens and whether there is a deeper logic behind it? I am still a C++ beginner, and since the compiler error only says "segmentation fault" without any additional information, I'm a bit at a loss here.
And also, how can I fix it? :-)
Any help highly appreciated!
PS: I know there are a gazillion threads about this topic. But that somehow makes it only harder to obtain useful information. I checked out the Wiki entry, but it does not seem to cover my problem.