Declaring 2d arrays globally and locally

Dec 20, 2014 at 6:38pm
Hello. In this example
1
2
3
4
5
int main()
{
    int A[3000][3000];
    return 0;
}


the program doesn't work, and as far as I understand it's because A is too large.

but

1
2
3
4
5
6
int A[3000][3000];

int main()
{
    return 0;
}


Here I declare A as global array, yes? In this case, everything works just fine, no errors, program returns 0.
Why is this happening? And if I want to use arrays with large dimensions,is declaring array globally acceptable?
Thank you.
Last edited on Dec 20, 2014 at 6:38pm
Dec 20, 2014 at 6:45pm
Actually the first one runs just fine. What error do you have?
Dec 20, 2014 at 6:48pm
It says "program.exe" has stopped working after I compile, and it returns big negative number
Dec 20, 2014 at 7:11pm
Quite strange... I just tried and nothing unusual happens.
Dec 20, 2014 at 7:48pm
Local variables are stored on the stack which is typically smaller than the space available for global variables. Your array takes 36,000,000 bytes for 32-bit integers and twice that for 64 bit integers.

You can probably set the stack size in your compiler options.
Topic archived. No new replies allowed.