【发布时间】:2016-09-09 17:23:44
【问题描述】:
我有这段代码,它是一个函数,用于在二维数组中进行渗透模拟。
int step (double ** mat, unsigned n, unsigned m, double a, double b)
{
int i, h, r, c, steps, x, y, o, v; // search for minimum
int min;
FILE *fp;
for(steps=0; steps<2; steps++) // percolation steps
{
for (o=0; o<n; o++)
{
for(v=0; v<m; v++)
{
if (mat[o][v]==-2) {mat[o][v]=-1;}
}
} //trasformo i -2 in -1
min=b; //set the minimum to max of range
for(x=0; x<n; x++) // i search in the matrix the invaded boxes
{
for(y=0; y<m; y++)
{
if (mat[x][y]=-1) //control for the boxes
{
if (mat[x][y-1]<=min && mat[x][y-1]>=0) {min=mat[x][y-1]; r=x; c=y-1;} //look for the minimum adjacent left and right
if (mat[x][y+1]<=min && mat[x][y+1]>=0) {min=mat[x][y+1]; r=x; c=y+1;}
for (i=-1; i<=1; i++) //look for the minimum adjacent up and down
{
for(h=-1; h<=1; h++)
{
if (mat[(x)+i][(y)+h]<=min && mat[(x)+i][(y)+h]>=0)
{
min=mat[(x)+i][(y)+h];
r=(x)+i; c=(y)+h;
}
}
}
}
}
}
mat[r][c]=-2;
x=r; y=c;
}
return 0;
}
当我在我的main 函数中使用它时,我得到Segmentation-fault (core dump created)。你知道错误在哪里吗?
【问题讨论】:
-
mat[x][y-1]和y = 0会导致未定义的行为。y = m和mat[x][y+1]相同。此外,使用=而不是==。在编译器中启用警告。 -
嗯,那是意大利面……
-
顺便说一句
if (mat[x][y]=-1)? -
@EugeneSh。 ...西..? ;)
-
已经入侵的细胞设置为-1。
标签: c segmentation-fault coredump