Here is my code where I declare a 2d array of size 2048 x 2048
#include <stdio.h>
int main(void)
{
int src[2048][2048];
return 0;
}
I am on Windows 11 machine with Cygwin installed. When I run gcc
, the code compiles, but when I run a.exe
, I get the following error.
0 [] a 1717 cygwin_exception::open_stackdumpfile: Dumping stack trace to a.exe.stackdump
But, if I declare a global array of the same size outside main function, and run the a.exe
, I do not get any error
#include <stdio.h>
int src[2048][2048];
int main(void)
{
return 0;
}
What could be happening here ? I have read few other posts here suggesting that large arrays should be created on heap using malloc
. In fact, if I use malloc
inside the function, I do not get any compile or runtime errors.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int r = 2048, c = 2048;
int* ptr = malloc((r * c) * sizeof(int));
for (int i = 0; i < r * c; i++)
ptr[i] = i + 1;
free(ptr);
return 0;
}
Thanks!