I study multithreading in C ++. Made a simple program to create and output a two-dimensional array. If the array has more than 50x50 elements, then the stream should work; if less, then it should work without threads. But when working with a stream, the program gives an error which is visible on the screenshot. Please tell me what is wrong. enter image description here

#include <iostream> #include <cstdlib> #include <thread> using namespace std; void arrayBig(int rowLength, int columnsLength) { int i, j; int arr[1000][1000]; for (i = 0; i <= rowLength - 1; i++) { for (j = 0; j <= columnsLength - 1; j++) { arr[i][j] = rand() % 10; } } for (i = 0; i <= rowLength - 1; i++) { for (j = 0; j <= columnsLength - 1; j++) { cout << arr[i][j] << " "; } cout << endl; } } int arrayLitle(int rowLength, int columnsLength) { int i, j; int arr[1000][1000]; for (i = 0; i <= rowLength - 1; i++) { for (j = 0; j <= columnsLength - 1; j++) { arr[i][j] = rand() % 10; } } for (i = 0; i <= rowLength - 1; i++) { for (j = 0; j <= columnsLength - 1; j++) { cout << arr[i][j] << " "; } cout << endl; } return arr[i][j]; } int main() { int columnsLength = 0, rowLength = 0; cout << "Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ Π΄Π»ΠΈΠ½Ρƒ массива!\n"; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ ряда: "; cin >> rowLength; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ столбцов: "; cin >> columnsLength; if ((rowLength >= 50) || (columnsLength >= 50)) { cout << "Π Π°Π±ΠΎΡ‚Π°ΡŽΡ‚ ΠΏΠΎΡ‚ΠΎΠΊΠΈ!" << endl; std::thread aB(arrayBig, rowLength, columnsLength); if (aB.joinable()) aB.join(); } else { cout << "Бправимся Π±Π΅Π· ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ²!!!" << endl; arrayLitle(rowLength, columnsLength) } system("pause"); return 0; } 

    1 answer 1

    Well, I would not call it multithreading ... You just do the same thing that is in the main thread in another. Anyway...

    But you have for

     int arr[1000][1000]; 

    roughly 4 megabytes of stack is required. Is it a bit much? try to allocate this memory dynamically, or practice on an array of, say, 100 to 100 ...

    • Thanks for the answer, it helped! Made a dynamic memory. I still do not really know what and how to multithreading, I just study the first day. Thanks again. - user220343 6:49 pm