The problem is that you are defining some huge objects locally. Local variables are created on the stack and the stack has limits (per thread). Sometimes the stack can be a maximum of 1 megabyte. Your arrays would be well beyond that. My guess is you are actually overflowing the stack. You could move the array definitions outside of main and your program should work as those arrays wouldn't be created on the stack. You could also define your arrays by making them static
in main
. This has the same effect as declaring them outside.
Globally defined variables (including uninitialized arrays) and static
uninitialized variables (even if they are in a function) generally get placed in a data segment and initialized when your program is run. They are also guaranteed to be set to all 0. This Wiki reference describes this data area in C as:
BSS in C
In C, statically-allocated objects without an explicit initializer are initialized to zero (for arithmetic types) or a null pointer (for pointer types). Implementations of C typically represent zero values and null pointer values using a bit pattern consisting solely of zero-valued bits (though this is not required by the C standard). Hence, the BSS section typically includes all uninitialized variables declared at file scope (i.e., outside of any function) as well as uninitialized local variables declared with the static keyword. An implementation may also assign statically-allocated variables initialized with a value consisting solely of zero-valued bits to the bss section.
The BSS
segment is not constrained like the stack is. BSS
could use up to available memory if the resources exist and you don't exceed any process quotas.
Another alternative is to dynamically allocate the arrays using malloc
which would put them on the heap. The following code would be the easiest way to create your arrays. I used #define
to make it clearer what is a row and column. After these arrays are defined and memory allocated they can be used like any normal 2D array.
#include<stdio.h>
#include<stdlib.h>
int main()
{
#define ROWS 1000
#define COLUMNS 1000
int test;
char (*a)[COLUMNS] = malloc(ROWS * sizeof *a);
int (*x)[COLUMNS] = malloc(ROWS * sizeof *x);
int (*y)[COLUMNS] = malloc(ROWS * sizeof *y);
a[100][20] = 'X';
x[4][999] = 666;
y[500][0] = 42;
scanf("%d",&test);
printf("%d",test);
free(a);
free(x);
free(y);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…