Possible Duplicate:
Could not allocate memory
My following code runs fine:
double weight [600] [800][3];
double mean [600] [800][3];
double sd [600] [800][3];
double u_diff [600] [800][3];
for ( int i = 0; i < 600; i ++ )
{
for ( int j = 0; j < 800; j ++ )
{
for ( int k=0; k < 3; m ++ )
{
weight [i][j][k] = 0;
mean[i][j][k] = 0;
sd[i][j][k] = 6;
}
}
}
But when I change it into this form:
int init = 6;
int C = 3;
for ( int i = 0; i < 600; i ++ )
{
for ( int j = 0; j < 800; j ++ )
{
for ( int k =0; k < 3; k ++ )
{
weight [i][j][k] = 1/C;
mean[i][j][k] = rand();
sd[i][j][k] = init;
}
}
}
it crashes. I even tried working for “weight”, “mean” and “sd” seperately. I doubt it might be of datatype, changed like:
double value = rand();
weight[i][j][m] = value;
but the error still remains. What is wrong here?
I got also the first version to crash (cygwin, 4.5.3).
The problem has to do with limited stack size, which has been around 2 MB.
Why it wouldn’t crash is probably due to optimization:
due to ‘rand’ in the other fragment, the optimizer/compiler couldn’t possibly
tell that the array is not used at all — which would very likely be visible
from the first fragment.
To get around the error, just allocate the large arrays from the heap with malloc
(or study the limit by having considerably smaller array 80x60x3 perhaps?)