gpt4 book ai didi

c - C中的大型二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:31 24 4
gpt4 key购买 nike

我尝试解决 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。有什么想法吗?

最佳答案

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

#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 程序中内存的排列方式的简要说明

关于c - C中的大型二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com