简体   繁体   中英

How define very large 2D array in C (like 1000*1000)

what is the solution? Do you know a way to help me create a large array? I used the (int) statement that creates a 100 * 100 array without problems, while this command for the 1000 * 1000 array causes error code.

#include<stdio.h>
#include<string.h>
#define true 1

int main()
{
    int i, j;

    int a[1000][1000];
    int b[1000][1000];
    int x = 0;
    i = 0;
    while (true)
    {
        scanf_s("%d", &b[i]);
        scanf_s("%c", &ch);
        if (ch !='-')
        {
            x = (i+1);
            break;
        }
        i++;
    }
    printf("%d\n\n", x);

    for (i = 0; i < x; i++)
    {
        for (j = 0; j < strlen(b[i]); j++)
        {
            printf("%d\n\n", b[i][j]);
        }
    }


return 0;
}

Compile the program as x64 not x86. In x86 heap size (microsoft compiler is limited to 2GB).

在此处输入图片说明

Choose x64

i changed it with malloc but it still has runtime error

#include<stdio.h>
#include<string.h>
#define true 1

int main()
{
    int n;
    scanf_s("%d", &n);
    int i, j;

    int** b = (int**)malloc(10000 * sizeof(int*));
    for (i = 0; i < 100000; i++)
    {
        b[i] = (int*)malloc(10000 * sizeof(int));
    }
    char ch;
    int x = 0;
    i = 0;
    while (true)
    {
        scanf_s("%d", &b[i]);
        scanf_s("%c", &ch);
        if (ch != '-')
        {
            x = (i + 1);
            break;
        }
        i++;
    }
    printf("%d\n\n", x);

    for (i = 0; i < x; i++)
    {
        for (j = 0; j < strlen(b[i]); j++)
        {
            printf("%d\n\n", b[i][j]);
        }
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM