'Value greater than 10^5 in 2d array was not declared in scope

When I tried:
int global[10000][10000];
it works but when
int global[100000][100000];
when I try to access it it gives me error:

global was not declared in the scope.

Any answers ?

Here is my test code:

#include<iostream>
#include<bits/stdc++.h>

int global[100000][100000];
using namespace std;

int main(){
    int i=0;
    while(i<=10){
        cout << global[i][i];
        i++;
    }
    return 0;
}


Solution 1:[1]

I tried reproducing the error in g++ 8.2.0 but it produces a different but linker error:

/tmp/cceGsEkP.o: In function `__static_initialization_and_destruction_0(int, int)':
temp.cxx:(.text+0x60): relocation truncated to fit: R_X86_64_32 against `.bss'
temp.cxx:(.text+0x6f): relocation truncated to fit: R_X86_64_32 against `.bss'

Above function is responsbile for allocation of static memory before main. Since array is allocated memory contiguously int global[10000][10000] may be possible because of memory requirement of around 300MB, but

Instead, you can:

  • Try to reduce dimension which will require you to remove redundant information which will not be used in further computation.
  • If you can't reduce the dimension but won't use all the states then you can use something like std::map.

Solution 2:[2]

I have the impression you want to show the ten first entries on the diagonal of a very large matrix. Why do you even want to do that?

  1. As you just need about 10 numbers, you can easily create a single-dimensioned matrix int global[10].
  2. Your matrix is so extremely large that it consumes your whole memory, as already mentioned in the comment of Blaze.
  3. Why do you call that variable global? What are you trying to achieve?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 madhur4127
Solution 2 Dominique