处理大型数组而不会出现运行时错误 [英] Dealing with large arrays without getting runtime error
问题描述
当我使用大小为10 ^ 5 * 10 ^ 5的数组时(例如int a[100000][100000]
),我遇到运行时错误.由于该数组消耗了更多的内存,这可能是导致运行时错误的原因之一.
I am getting runtime error when I used array of size 10^5*10^5 (ex. int a[100000][100000]
. As this array is consuming more memory, this may be one one of the reasons for runtime error.
我应该如何声明该数组(二维数组,例如int a[100000][100000]
),以便可以解决SIGSEGV
运行时错误的问题?
How should I declare this array (2D array ex. int a[100000][100000]
) so that I can cope with problem of SIGSEGV
runtime error?
请提供声明这种类型的数组的方法吗?
Please, provide approach to declare such type of array?
推荐答案
声明大数组int a[100000][100000]
的三种方法是:
Three ways to declare the large array int a[100000][100000]
are:
- 将大型阵列设为全局
-
制作大型数组
static
:
static int a[100000][100000];
使用malloc
/calloc
并动态分配大数组:
Use malloc
/calloc
and dynamically allocate the large array:
int **a;
a=malloc(sizeof(int*)*100000);
for(int i=0;i<100000;i++)
a[i]=malloc(sizeof(int)*100000);
/*Use the array*/
for(int i=0;i<100000;i++)
free(a[i]);
free(a);
这篇关于处理大型数组而不会出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!