C中的大型2d阵列

时间:2015-12-17 21:39:29

标签: c arrays list

我尝试解决4个变量和600000个约束的线性优化问题。 我需要生成一个大输入。因此,我需要A[600000][4]用于约束的系数,b[600000]用于正确的部分。这是一个生成600000约束的代码。

int i, j;
int numberOfInequalities = 600000;
double c[4];
double result[4];;
double A[numberOfInequalities][4], b[numberOfInequalities];

printf("\nPreparing test: 4 variables, 600000 inequalities\n");
A[0][0] = 1.0; A[0][1] = 2.0; A[0][2] = 1.0; A[0][3] = 0.0; b[0] = 10000.0;
A[1][0] = 0.0; A[1][1] = 1.0; A[1][2] = 2.0; A[1][3] = 1.0; b[1] = 10000.0;
A[2][0] = 1.0; A[2][1] = 0.0; A[2][2] = 1.0; A[2][3] = 3.0; b[2] = 10000.0;
A[3][0] = 4.0; A[3][1] = 0.0; A[3][2] = 1.0; A[3][3] = 1.0; b[3] = 10000.0;
c[0]=1.0; c[1]=1.0; c[2]=1.0; c[3]=1.0;
for( i=4; i< 100000; i++ )
{
    A[i][0] = (12123*i)%104729;
    A[i][1] = (47*i)%104729;
    A[i][2] = (2011*i)%104729;
    A[i][3] = (7919*i)%104729;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1 + (i%137);
}
A[100000][0] = 0.0; A[100000][1] = 6.0; A[100000][2] = 1.0;
A[100000][3] = 1.0; b[100000] = 19.0;
for( i=100001; i< 200000; i++ )
{
    A[i][0] = (2323*i)%101111;
    A[i][1] = (74*i)%101111;
    A[i][2] = (2017*i)%101111;
    A[i][3] = (7915*i)%101111;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%89);
}
A[200000][0] = 5.0; A[200000][1] = 2.0; A[200000][2] = 0.0;
A[200000][3] = 1.0; b[200000] = 13.0;
for( i=200001; i< 300000; i++ )
{
    A[i][0] = (23123*i)%100003;
    A[i][1] = (47*i)%100003;
    A[i][2] = (2011*i)%100003;
    A[i][3] = (7919*i)%100003;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%57);
}
A[300000][0] = 1.0; A[300000][1] = 2.0; A[300000][2] = 1.0;
A[300000][3] = 3.0; b[300000] = 20.0;
A[300001][0] = 1.0; A[300001][1] = 0.0; A[300001][2] = 5.0;
A[300001][3] = 4.0; b[300001] = 32.0;
A[300002][0] = 7.0; A[300002][1] = 1.0; A[300002][2] = 1.0;
A[300002][3] = 7.0; b[300002] = 40.0;
for( i=300003; i< 400000; i++ )
{
    A[i][0] = (13*i)%103087;
    A[i][1] = (99*i)%103087;
    A[i][2] = (2012*i)%103087;
    A[i][3] = (666*i)%103087;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1;
}
for( i=400000; i< 500000; i++ )
{
    A[i][0] = 1;
    A[i][1] = (17*i)%999983;
    A[i][2] = (1967*i)%444443;
    A[i][3] = 2;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + (1000000.0/(double)i);
}
for( i=500000; i< 600000; i++ )
{
    A[i][0] = (3*i)%111121;
    A[i][1] = (2*i)%999199;
    A[i][2] = (2*i)%444443;
    A[i][3] = i;
    b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1.3;
}

问题是:它无法创建如此大的数组,它只是在运行时终止,但如果我创建的限制不超过200000,它就能正常工作。 我试图将堆栈大小增加到无限值,但它没有帮助。 我尝试使用像** A这样的指针,但输出结果不正确。 附: 我用的是Ubuntu。 有什么想法吗?

1 个答案:

答案 0 :(得分:3)

如果numberOfInequalities是运行时常量,则可以将其设为#define并将Ab定义为全局变量或static局部变量:< / p>

#define numberOfInequalities 600000
static double A[numberOfInequalities][4], b[numberOfInequalities];

这会将这些数组从“堆栈”移动到“bss”段。

更好的解决方案是使用malloc分配这些数组:

double (*A)[4] = malloc(numberOfInequalities * 4 * sizeof(double));
double *b = malloc(numberOfInequalities * sizeof(double));

这将导致从“堆”内存分配这些数组。 在返回呼叫者之前不要忘记释放它们。

请参阅http://www.geeksforgeeks.org/memory-layout-of-c-program/,以简要说明如何在典型的C程序中安排内存