0

I have the following code:

#include <bits/stdc++.h>
using namespace std;

const int MAX=25;
const int TMAX=1 << MAX - 1;
double D[TMAX][MAX];

int main(){}

If I compile it I get

champ@champ-S451LN:~/code$ g++   kike2.cpp 
/tmp/ccuK5NOq.o: In function `__static_initialization_and_destruction_0(int, int)':
kike2.cpp:(.text+0x717): relocation truncated to fit: R_X86_64_PC32 against `.bss'
kike2.cpp:(.text+0x72a): relocation truncated to fit: R_X86_64_PC32 against `.bss'
collect2: error: ld returned 1 exit status

If I make MAX=22 I do not get this error, I think the problem is that TMAX*MAX exceeds 2^32.

Having access to such big 2D arrays would be useful for me. Does anyone know how to make them?

Here is what I did in the end:

#include <bits/stdc++.h>
using namespace std;

double** D = new double*[TMAX];// this is the DP array (its big so we save it differently)

int main(){
    for(int i = 0; i < TMAX; ++i){// store the big array
        D[i] = new double[MAX];
    }
}

2 Answers2

2

You cannot make them really large on the stack, as the stack is nearly always severly more limited than main meory.

Use malloc or new instead, to create the object on the heap; main memory will be the only limit, including use of swap files if you want to go on that limb.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • Thank you very much. I think I understand but could you add a line of code saying how you would do it explicitly please? –  May 09 '17 at 02:52
  • What do you recommend new or malloc? –  May 09 '17 at 02:55
  • See [here](http://stackoverflow.com/a/936709/4096667) (You do realize that this is going to ask the system for more than 3 gigs of RAM all at once, right?) – A C May 09 '17 at 02:56
  • 1
    `D` is a global variable, not on the stack. – 1201ProgramAlarm May 09 '17 at 03:12
  • @AC it worked fine and my computer has 4gb of ram. Are you sure you arent overestimating? –  May 09 '17 at 03:22
  • 1
    @1201ProgramAlarm Yeah - I guess the error is indicating that it's actually too big for the BSS segment, but Aganju's solution should still work. And yeah, Jorge, my math could have been off or your computer could be good at using swap space, either way glad it worked! – A C May 09 '17 at 03:25
0

You must use it in heap, rather than stack.

double* D = (double*)malloc(sizeof(double) * TMAX * MAX);
// D[d1][d2] can be accessed *(D + d1 * TMAX + d2)
Jcppython
  • 179
  • 12