分割过错二维数组 [英] segmentation fault for 2D arrays

查看:191
本文介绍了分割过错二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要定义非常大大小的二维数组。但它给我段错误?

I want to define a 2D array of very big size. But it is giving me segmentation fault?

  #include <stdio.h>

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

任何人都可以请建议我一些其他的方式?有一些问题,内存初始化?在此先感谢

Can anyone please suggest me some other way? Is there some problem with memory initialization? Thanks in advance

推荐答案

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

You can allocate the whole table in only one array but you won't be able to access array data with indices using two square brackets:

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

访问,其中previously你写温度由[i] [j]的,现在你现在应该计算该指数通过以下方式元素(I,J)

to access the element (i,j) where previously you wrote temp[i][j], now you should now compute the index the following way:

temp[i*5000+j];

和不要忘记释放分配给你的表后记忆:

and do not forget to free the memory allocated for your table afterward:

free(temp);

这篇关于分割过错二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆