tg_91
tg_91

Reputation: 43

Segmentation fault (core dumped) - unreachable counter value

I need to use two-dimensional table of boolean that helps me resolve problem with dynamic-programming method. Unfortunately I encountered an issue, where I want to print counted results. I didn't code much with c++ before so I don't understand what's wrong with this code, especially since I'm not using here any custom made structs, classes or pointers and I double checked if array bounds in iterators are correct.

Here's the simple code that's an example of my issue :

const short int N=1001;
const short int M=10001;

int main() {
    bool tab[N][M];
    for (int i=0;i<N;i++)
        for (int j=0;j<M;j++)
            tab[i][j]=false;

    int foo=0;
    for (int i=0;i<N;i++)
        for (int j=0;j<M;j++)
            if (!tab[i][j])
                foo++;

    cout << foo << endl;

    return 0;
}

Why trying to print foo value gets segmentation fault error? That variable is initialised before I try increasing it in the for function. When I remove the cout line everything is ok and program does finish the work (though I can't then see the results). Thanks in advance for helping me to solve this (most likely) simple issue.

Upvotes: 1

Views: 213

Answers (1)

alex
alex

Reputation: 161

There is a segmentation fault error because you are trying to store a number too big for an integer variable.

You are trying to store 10011001 (1001 * 10001) in an integer variable when the maximum value it can store is 32767 (http://www.cplusplus.com/reference/climits/)

Upvotes: 2

Related Questions