为数组赋值时为什么会崩溃? [英] why it crashes when assigning new values to arrays?
问题描述
可能重复:
无法分配内存
我的下面的代码运行良好:
双重重量[600] [800] [3];
double mean [600] [800] [3];
double sd [600] [800] [3];
double u_diff [600] [800] [3]; (int j = 0; j <800; j ++)
(int i = 0; i <600; i ++)
{
(int k = 0; k <3; m ++)
{
weight [i] [j] [k] = 0;
mean [i] [j] [k] = 0;
sd [i] [j] [k] = 6;
$ / code $ / pre
但是当我改变它这个表单:
int init = 6;
int C = 3; (int j = 0; j <800; j ++)
(int i = 0; i <600; i ++)
{
(int k = 0; k <3; k ++)
{
weight [i] [j] [k] = 1 / C; b $ b {
mean [i] [j] [k] = rand();
sd [i] [j] [k] = init;
崩溃。我甚至尝试过为重量,平均值和SD分别工作。我怀疑它可能是数据类型,如下所示:
double value = rand();
weight [i] [j] [m] = value;
但是错误仍然存在。这里有什么问题?
解决方案
我还得到了第一个崩溃的版本(cygwin,4.5.3)。
这个问题与有限的堆栈大小有关,大小约为2 MB。
为什么不会崩溃可能是由于优化:
由于'rand'在另一个片段中,优化器/编译器不可能在
中告诉数组根本没有被使用 - 这很可能是可见的< b
gcc -std = c99 tst.c -O&& ./a.exe - 不会产生
gcc -std = c99 tst.c&& ./a.exe - 分段错误
为了解决这个错误, malloc
的堆(或者研究一下极小的数组80x60x3也许会限制这个限制?)
// tst。 c
//编译并运行gcc -std = c99 tst.c -DOK = 0 -DW = 80 -DH = 60&& ./a.exe // ok
//或gcc -std = c99 tst.c -DOK = 0 -DW = 800 -DH = 600&& ./a.exe //崩溃
//或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)); //没有崩溃
#else
双重重量[W * H * 3]; //当W * H很大时崩溃,W * H很小的时候崩溃
#endif
int z = 0; (int j = 0; j< H; j ++)
{
for(int i = 0; i< W; i ++) (int m = 0; m <3; m ++)
{
weight [z ++] = 0;
}
}
}
return 0;
}
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.
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;
}
这篇关于为数组赋值时为什么会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!