什么会导致此函数中的堆栈溢出?
What can cause a Stack Overflow in this function?
我在写一个休闲扫雷游戏,想实现一种在野外追踪空单元格的方法,所以我写了这个算法:
//bigger array was taken to prevent out of range,when init mines and numbers
/*creating mines in 1 to FIELD_NUM range(0 -non-active field,1-active field)
* {
* 0 0 0 0 0 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 0 0 0 0 0
* }
*/
//view is a display vect,x and y are mouse interp. coord.
void MinerField::OpenCell(vector<vector<int>>& view, int x, int y)
{
if (gridLogic[x][y] == 9)
{
for (int i = 1; i <= FIELD_NUM; i++)
for (int j = 1; j <= FIELD_NUM; j++)
{
view[i][j] = gridLogic[i][j];
}
}
else
{
if (gridLogic[x][y] == 0)
OpenVoidCells(view, x, y);
else
view[x][y] = gridLogic[x][y];
}
}
第二个函数导致堆栈溢出:
void MinerField::OpenVoidCells(vector<vector<int>>& view, int x, int y)
{
if (x >= (FIELD_NUM) || y >= (FIELD_NUM))//check out of range
return;
if (gridLogic[x][y] == 10 || gridLogic[x][y]==11 ||gridLogic[x][y]==-1)
return;
if ((gridLogic[x][y] <= 8) && (gridLogic[x][y] >= 1))
{
view[x][y] = gridLogic[x][y];
return;
}
view[x][y] = gridLogic[x][y];
OpenVoidCells(view,x + 1, y); //North;
OpenVoidCells(view,x - 1, y); //South
OpenVoidCells(view,x, y + 1); //East
OpenVoidCells(view, x, y - 1); //West
OpenVoidCells(view, x - 1, y - 1); //South-West
OpenVoidCells(view, x + 1, y + 1); //North-East
OpenVoidCells(view, x - 1, y + 1); //South-East
OpenVoidCells(view, x + 1, y - 1); //North-West
}
gridLogic
向量是 MinerField
局部的并且与 view
具有相同的大小。 运行 失败并返回 FIELD_NUM=10
。
什么会导致堆栈溢出?
OpenVoidCells
没有任何东西可以防止一遍又一遍地访问同一个广场。它会一直向北、向南、向北、向南、向北、向南……直到你 运行 离开堆栈。您需要跟踪访问过的方块并避免重新检查它们。
我在写一个休闲扫雷游戏,想实现一种在野外追踪空单元格的方法,所以我写了这个算法:
//bigger array was taken to prevent out of range,when init mines and numbers
/*creating mines in 1 to FIELD_NUM range(0 -non-active field,1-active field)
* {
* 0 0 0 0 0 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 1 1 1 1 0
* 0 0 0 0 0 0
* }
*/
//view is a display vect,x and y are mouse interp. coord.
void MinerField::OpenCell(vector<vector<int>>& view, int x, int y)
{
if (gridLogic[x][y] == 9)
{
for (int i = 1; i <= FIELD_NUM; i++)
for (int j = 1; j <= FIELD_NUM; j++)
{
view[i][j] = gridLogic[i][j];
}
}
else
{
if (gridLogic[x][y] == 0)
OpenVoidCells(view, x, y);
else
view[x][y] = gridLogic[x][y];
}
}
第二个函数导致堆栈溢出:
void MinerField::OpenVoidCells(vector<vector<int>>& view, int x, int y)
{
if (x >= (FIELD_NUM) || y >= (FIELD_NUM))//check out of range
return;
if (gridLogic[x][y] == 10 || gridLogic[x][y]==11 ||gridLogic[x][y]==-1)
return;
if ((gridLogic[x][y] <= 8) && (gridLogic[x][y] >= 1))
{
view[x][y] = gridLogic[x][y];
return;
}
view[x][y] = gridLogic[x][y];
OpenVoidCells(view,x + 1, y); //North;
OpenVoidCells(view,x - 1, y); //South
OpenVoidCells(view,x, y + 1); //East
OpenVoidCells(view, x, y - 1); //West
OpenVoidCells(view, x - 1, y - 1); //South-West
OpenVoidCells(view, x + 1, y + 1); //North-East
OpenVoidCells(view, x - 1, y + 1); //South-East
OpenVoidCells(view, x + 1, y - 1); //North-West
}
gridLogic
向量是 MinerField
局部的并且与 view
具有相同的大小。 运行 失败并返回 FIELD_NUM=10
。
什么会导致堆栈溢出?
OpenVoidCells
没有任何东西可以防止一遍又一遍地访问同一个广场。它会一直向北、向南、向北、向南、向北、向南……直到你 运行 离开堆栈。您需要跟踪访问过的方块并避免重新检查它们。