0

As the title says - I have a really basic piece of code which should read two numbers, and then a 2D float array. I get no error but when I run my program, it crashes with code -1073741571 and I can't understand why.

Here's the code:

#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

void citireNrEcuatiiNecunoscuteSiMatriceaExtinsa(int& a, int& b, float c[][1001])
{
    cout << "\nIntroduceti numarul de ecuatii: ";
    cin >> a;
    cout << "\nIntroduceti numarul de necunoscute: ";
    cin >> b;
    int i = a;
    int j = b + 1;
    cout << "\nIntroduceti elementele matricei extinse: ";
    for (int contorLinie = 0; contorLinie < i; contorLinie++)
    {
        for (int contorColoana = 0; contorColoana < j; contorColoana++)
        {
            cin >> c[contorLinie][contorColoana];
        }
    }
}

int main()
{
    int numarEcuatii, numarNecunoscute;
    cout << setprecision(3);
    float mat[1001][1001];
    citireNrEcuatiiNecunoscuteSiMatriceaExtinsa(numarEcuatii, numarNecunoscute, mat);
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 3
    `float mat[1001][1001]` is bloody massive. That's surely more than you have room for on the stack. – Carcigenicate Dec 22 '20 at 19:13
  • Actually, that's not quite as large as I thought it was. With a 4-byte float, that's 4mb, so whether you can do that depends on how large of a stack you have set. – Carcigenicate Dec 22 '20 at 19:14
  • 1
    @Carcigenicate: He coded it on Windows. The stack size of the main thread is 1mb unless he changed it. (It's in the project file so you won't see it here.) – Joshua Dec 22 '20 at 19:20
  • This is a duplicate of [this](https://stackoverflow.com/q/64403833). However, that question has been closed with duplicates to other questions that do not match. – JHBonarius Dec 22 '20 at 20:19
  • @Carcigenicate that was the problem. Changing [1001] to [101] worked. Thanks :) –  Dec 23 '20 at 06:07

3 Answers3

3

You get a stack overflow. You should use new [] operator to allocate the array in dynamic memory or just use std::vector (which will be simpler, 'cause you don't need to allocate every internal array using for cycle).

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
1

If allocating huge chunks of memory, consider doing so on heap. Doing so is the safest bet that memory is allocated to you, given you are freeing (delete in C++) the allocated memory later to avoid any memory leaks.

float **mat = new float[1001][1001];
Jarvis
  • 8,494
  • 3
  • 27
  • 58
0

As @Jarivs mentioned the way to allocate 2d memory, but it is not as simple as he mentioned. To do that you have to do this.

To declare 2d array on heap you can do like this:

    float **mat = new float*[1001];
    for(int i = 0; i < 10001; ++i){
        mat[i] = new float[1001];
    }

but do not forget to delete, and memory you allocate to multi dimensional array can be freed this way.

    for(int i = 0; i < 10001; ++i ){
        delete [] mat[i];
    }
foragerDev
  • 1,307
  • 1
  • 9
  • 22