-1

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.

Sacha
  • 111
  • 4
  • Possible duplicate of [Segmentation fault on large array sizes](http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes?rq=1), and *hundreds* of similar questions. – WhozCraig Feb 28 '15 at 21:17
  • Recognizing stack overflows can sometimes be difficult. Using posix on Linux, the stack size is available using "::pthread_attr_getstack(3 parameters)". My 12.04 system reports that the thread default is 8,388,608 bytes, quite a bit smaller than (3000*3000) booleans. – 2785528 Mar 01 '15 at 01:37

2 Answers2

0

Probably you're trying to reserve too much memory in your main() function (I mean coprimes array, 3000x3000 of bool it is more than 8MB). See here for more details.

Community
  • 1
  • 1
Marcin Kolny
  • 633
  • 7
  • 14
0

There is a stack overflow issue because of too big coprimes matrix. You can allocate memory dynamically on heap (don't forget to release).

bool **coprimes = new bool*[N];
for (int i = 0; i < N; ++i)
    coprimes[i] = new bool[N];
  • Thank you. There is also the [auto keyword](http://stackoverflow.com/a/16239446/4500580) keyword that seems to do the same thing in C++11. But still ... why does the segmentation only occur when I include the line std::cout << c << std::endl;? – Sacha Mar 01 '15 at 07:49
  • Actually it shouldn't depend on `std::cout << c << std::endl;`. At least in my compiler program terminates without this line as well. Do you have any optimization on? – Andrey Derevyanko Mar 01 '15 at 08:07
  • Hrgh ... yes. That was it. Thanks a lot. I was jusing the -O3 compiler flag. Without it, the segmentation fault occurs with or without said line. Thanks a lot, I learned an important lesson: Remove optimization flags to debug. – Sacha Mar 01 '15 at 09:15