The program crashes with the following error:

Unhandled exceptions at 0x00821CC9 in p.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x006E2000)

What is causing it?

Program Code:

#include <iostream> #include <vector> using namespace std; int main() { long long v[1901][2015]; long long p[1901][2015]; long long i, j, k = 1, n = 0; for (i = 0; i < 1901; ++i) for (j = 0; j < 2015; ++j) { p[i][j] = k; ++k; } k = 1; for (i = 0; i < 2015; ++i) for (j = 0; j < 1901; ++j) { v[j][i] = k; ++k; } for (i = 0; i < 1901; ++i) for (j = 0; j < 2015; ++j) if (v[i][j] == p[i][j]) ++n; cout << n; system("pause"); return 0; } 

    1 answer 1

    Your problem is generally understandable and without details, but next time do not be lazy to write more.

    You have too large arrays for the function. Inside the function, only a certain amount of memory is available. Bring these arrays to global scope.

     #include <iostream> #include <vector> long long v[1901][2015]; long long p[1901][2015]; int main() { long long i, j, k = 1, n = 0; for (i = 0; i < 1901; ++i) for (j = 0; j < 2015; ++j) { p[i][j] = k; ++k; } k = 1; for (i = 0; i < 2015; ++i) for (j = 0; j < 1901; ++j) { v[j][i] = k; ++k; } for (i = 0; i < 1901; ++i) for (j = 0; j < 2015; ++j) if (v[i][j] == p[i][j]) ++n; std::cout << n; system("pause"); return 0; } 

    PS I do not recommend using namespace std; , write better std:: when you write large projects, they will have many namespaces and you will understand that it is more convenient.

    • Thank you very much, I will take your advice. - Paul Shipilov