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?)
I tried to build the following code in Cygwin(1.7.15) and VC++ compiler, But Didn't get any crash. It works fine for me.
double weight [600] [800][3];
double mean [600] [800][3];
double sd [600] [800][3];
double u_diff [600] [800][3];
int init = 6;
int C = 3;
int main()
{
int i = 0;
for ( i = 0; i < 600; i ++ )
{
int j = 0;
for ( j = 0; j < 800; j ++ )
{
int k = 0;
for ( k=0; k < 3; k ++ )
{
weight [i][j][k] = 1/C;
mean[i][j][k] = rand();
sd[i][j][k] = init;
}
}
}
return 0;
}
What's
k
? Did you mean?
Also,
1/C
forC=3
is 0, because they are bothint
s. Perhaps you wanted1.0/C
?Answer after edits and comments:
Those arrays are pretty huge. You should allocate them dynamically.