SIGSEV Error.!

I am solving a question and I have declared 4 arrays-- 3 1-Dimensional and 1 2-Dimensional Array. The value of N i.e the size of the array can be at max 10^5. The sizes of all the arrays are declared after taking the input.

Code:-

     #include<iostream>
     int main()
    {
      int a,b;
      cin >> a >> b;
      int arr[a][b];
      for(int i=0;i<a;i++)
      {
          for(int j=0;j<b;j++)
              cin >> arr[i][j];
      }
     return 0;
     }

The values of and b can be at MAX 10^5. The other three arrays can also have size at max 10^5 and are in terms of b i.e
int arr[b].
However I am getting a SIGSEV Error. Any tips to optimize this piece of code?

Here the Array out of bound Exception is been thrown which is obvious because you are declaring the array size of total 10^{10}. Since the max size of array allowed is 10^{8} so the only possibility to make your code work properly is that you must use single dimension array which would definitely fit without giving SIGSEV Error.

Happy Coding!

1 Like

That was too naive of me not to think that the size of the 2D array was exceeding the limit. Thank you. You pointed me in the right direction.!

Out of bound Exception occurs due to the limited memory space assigned to the array…So you are not allowed use the rest space for the same

1 Like

@bansal1232 the max size allowed is usually 10^6 - 10^7

1 Like

@mathecodician we can easily declare the max size of array even up to 10^9 if we declare it globally i.e., outside main() function or declare it statically inside main() function because when an array[] declared inside the function, it’s allocated in stack but when you declare this at outside then it is statically allocated in your computer’s virtual memory or in heap by the OS linker/loader and hence giving you ability to store up-to max size of array[]

So you will not got Run-time error if you declare like that!

Will this work for 2-D arrays with 10^9-10^10 elements?

No! it will not work

@vijju123 2D array works upto range of 10^6 ie row*col ie number of cells should be @ max of range 10^6