处理大型数组而不会出现运行时错误 [英] Dealing with large arrays without getting runtime error

查看:67
本文介绍了处理大型数组而不会出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用大小为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:

  1. 将大型阵列设为全局
  2. 制作大型数组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屋!

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