繁体   English   中英

在C ++中制作大型2D数组

[英]Making a big 2d array in c++

我有以下代码:

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

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

int main(){}

如果我编译它,我得到

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

如果我使MAX = 22我没有收到此错误,我认为问题是TMAX * MAX超过2 ^ 32。

能够访问这么大的2D阵列对我来说将是有用的。 有人知道怎么做吗?

这是我最后所做的:

#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];
    }
}

您不能使它们在堆栈上真正变大,因为堆栈几乎总是比主要内存要有限得多。

使用mallocnew可以在堆上创建对象; 主内存将是唯一的限制,如果要继续使用,则包括使用交换文件。

您必须在堆而不是堆栈中使用它。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM