gpt4 book ai didi

c - 二维数组的段错误

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

我想定义一个非常大的二维数组。但它给我段错误?

  #include <stdio.h>

int main () {
int i;
int temp[4000][5000];
for (i = 0; i < 5; i++)
{
printf ("Hello World\n");
}
}

谁能给我一些其他的建议?内存初始化有问题吗?提前致谢

最佳答案

您可以将整个表分配到一个数组中,但您将无法使用两个方括号访问带有索引的数组数据:

int * temp = malloc(4000*5000*sizeof(int));

要访问您之前编写的 temp[i][j] 元素 (i,j),现在您应该按以下方式计算索引:

temp[i*5000+j];

不要忘记释放分配给你的表的内存:

free(temp);

关于c - 二维数组的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19584758/

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