C++ DFS游戏求解程序偶发0xC0000005错误及数组值异常
问题描述
我学完Java后转攻C++,写了个用DFS算法求解某游戏的程序,运行时出现异常:有时程序直接抛出0xC0000005错误,有时能正常运行但state数组会被填充随机整数。我推测是对C++内存分配不熟悉导致的问题,相关代码如下(假设data是已初始化的7x7数组,元素为0、1、2、3):
#include <iostream> using namespace std; #include <stack> void printArray(int arr[][7]) { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) cout << arr[i][j] << ", "; cout << '\n'; } cout << '\n'; } bool solution (int arr[][7]) { for (int i = 0; i < 7; i++) for(int j = 0; j < 7; j++) if (arr[i][j] == 0) return false; return true; } int main() { int data[7][7]; printArray(data); stack<int(*)[7][7]> nodes;//stack of each possible gamestate nodes.push(&data); while (nodes.size() > 0) { int (*statePtr)[7][7] = nodes.top();//accessing top element as pointer nodes.pop(); int state[7][7]; for (int i = 0; i < 7; i++)//passing into 2d array by value for (int j = 0; j < 7; j++) state [i][j] = (*statePtr[i][j]); if (solution(state)) { printArray(state); break; } for (int i = 0; i < 7; i++)//for all empty values in state for (int j = 0; j < 7; j++) if (state[i][j] == 0) { bool existNeigh1, existNeigh2, existNeigh3; existNeigh2 = false; existNeigh3 = false; int neighbors[8][2] = { {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; for (int n = 0; n < 8; n++)//checking if this cell has a 1, 3, 5 neighbor { if (i+neighbors[n][0] >= 0 && i+neighbors[n][0] < 7 && j+neighbors[n][1] >= 0 && j+neighbors[n][1] < 7) { int neighborValue = state[i+neighbors[n][0]][j+neighbors[n][1]]; if (neighborValue == 1) existNeigh1 = true; if (neighborValue == 2) existNeigh2 = true; if (neighborValue == 3) existNeigh3 = true; } } if (existNeigh1)//if so, adding an updated board to the stack { int stateCopy[7][7]; for(int r = 0; r < 7; r++) for(int c = 0; c < 7; c++) stateCopy[r][c] = state[r][c]; stateCopy[i][j] = 1; nodes.push(&stateCopy); //printArray(stateCopy); } if (existNeigh2) { int stateCopy[7][7]; for(int r = 0; r < 7; r++) for(int c = 0; c < 7; c++) stateCopy[r][c] = state[r][c]; stateCopy[i][j] = 2; nodes.push(&stateCopy); //printArray(stateCopy); } if (existNeigh3) { int stateCopy[7][7]; for(int r = 0; r < 7; r++) for(int c = 0; c < 7; c++) stateCopy[r][c] = state[r][c]; stateCopy[i][j] = 3; nodes.push(&stateCopy); //printArray(stateCopy); } } } return 0; }
问题根源
- 局部变量生命周期错误:在
existNeigh1等分支里定义的stateCopy是栈上的局部数组,代码块执行完后,这块内存会被系统回收。但你把&stateCopy(局部数组的地址)push进了栈nodes,后续取出指针访问时,对应的内存已经不属于这个数组,要么读到垃圾数据,要么触发非法内存访问(0xC0000005错误)。 - 指针优先级错误:
(*statePtr[i][j])的写法有问题,[]的优先级比*高,实际执行的是*(statePtr[i][j]),会导致数组越界访问,正确写法是(*statePtr)[i][j]。 - 未初始化变量:
existNeigh1没有初始化,会导致随机布尔值,影响后续逻辑判断。
修复方案
- 用容器替代指针存储状态
不要用指针存栈上数组的地址,改用std::array<std::array<int,7>,7>直接存储状态对象,C++会自动管理对象的拷贝和生命周期,避免野指针问题。 - 修正指针访问优先级
将state [i][j] = (*statePtr[i][j]);改为state[i][j] = (*statePtr)[i][j];,确保先解引用指针再访问数组元素。 - 初始化所有局部变量
把bool existNeigh1, existNeigh2, existNeigh3;改为bool existNeigh1 = false, existNeigh2 = false, existNeigh3 = false;,避免随机值干扰逻辑。
修复后的示例代码
#include <iostream> #include <stack> #include <array> using namespace std; void printArray(const array<array<int,7>,7>& arr) { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) cout << arr[i][j] << ", "; cout << '\n'; } cout << '\n'; } bool solution(const array<array<int,7>,7>& arr) { for (int i = 0; i < 7; i++) for(int j = 0; j < 7; j++) if (arr[i][j] == 0) return false; return true; } int main() { array<array<int,7>,7> data; // 此处假设data已被正确初始化 printArray(data); stack<array<array<int,7>,7>> nodes; nodes.push(data); while (!nodes.empty()) { auto state = nodes.top(); nodes.pop(); if (solution(state)) { printArray(state); break; } for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) if (state[i][j] == 0) { bool existNeigh1 = false, existNeigh2 = false, existNeigh3 = false; int neighbors[8][2] = { {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; for (int n = 0; n < 8; n++) { int ni = i + neighbors[n][0]; int nj = j + neighbors[n][1]; if (ni >= 0 && ni < 7 && nj >=0 && nj <7) { int neighborValue = state[ni][nj]; if (neighborValue == 1) existNeigh1 = true; if (neighborValue == 2) existNeigh2 = true; if (neighborValue == 3) existNeigh3 = true; } } if (existNeigh1) { auto stateCopy = state; stateCopy[i][j] = 1; nodes.push(stateCopy); } if (existNeigh2) { auto stateCopy = state; stateCopy[i][j] = 2; nodes.push(stateCopy); } if (existNeigh3) { auto stateCopy = state; stateCopy[i][j] = 3; nodes.push(stateCopy); } } } return 0; }
内容的提问来源于stack exchange,提问作者Aryan Yoshi Joshi
相关产品推荐
相关产品推荐

