why it crashes when assigning new values to arrays

2019-08-20 05:36发布

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?

3条回答
Melony?
2楼-- · 2019-08-20 06:02

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.

gcc -std=c99 tst.c -O  && ./a.exe -- produces nothing
gcc -std=c99 tst.c && ./a.exe -- segmentation fault

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?)

// tst.c
// compile and run with gcc -std=c99 tst.c -DOK=0 -DW=80 -DH=60 && ./a.exe    // ok
//               or     gcc -std=c99 tst.c -DOK=0 -DW=800 -DH=600 && ./a.exe  // crash
//               or     gcc -std=c99 tst.c -DOK=1 -DW=800 -DH=600 && ./a.exe  // ok
#include <stdlib.h>
int main()
{
#if OK
    double *weight =(double*)malloc(W*H*3*sizeof(double));      // no crash
#else
    double weight[W*H*3];   // crash when W*H is large, nocrash when W*H is small
#endif
    int z=0;
    for ( int i = 0; i < W; i ++ )
    {
        for ( int j = 0; j < H; j ++ )
        {
            for ( int m =0; m < 3; m ++ )
            {
                 weight[z++]=0;     
            }       
        }
    }
    return 0;
}
查看更多
做个烂人
3楼-- · 2019-08-20 06:15

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; }

查看更多
爷的心禁止访问
4楼-- · 2019-08-20 06:18
mean[i][j][k] = rand(); 

What's k? Did you mean

mean[i][j][m] = rand(); 

?

Also, 1/C for C=3 is 0, because they are both ints. Perhaps you wanted 1.0/C?

Answer after edits and comments:

Those arrays are pretty huge. You should allocate them dynamically.

double* mean = new double[600*800*3];
// in the inner loop:
    mean[ k + 800*( j + 600*i )] = rand();

// when you're done with them
delete[] mean;
查看更多
登录 后发表回答