动态二维数组元素不可访问几个for循环
Dynamic 2D array elements are not accessible several for loop
我学过数据结构和算法,我遇到了动态二维数组的问题。这是我的代码的一部分。编码问题是骑士之旅
int iMove[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int jMove[8] = { 1, 2, 2, 1,-1, -2, -2, -1};
cell* cellList(int* i, int* j, int** board){
int k;
cell* temp;
int iTempNext; int jTempNext;
int maxSampleNum = 8;
int cnt = 0;
int val;
for(k = 0; k < maxSampleNum; k++){
iTempNext = (*i) + iMove[k];
jTempNext = (*j) + jMove[k];
//1. get list 0<=i<=7 && 0<=j<=7
if( (0 <= iTempNext && iTempNext <= 7) && (0 <= jTempNext && jTempNext <= 7)){
//2. get the 0 value cells
//val = canMove(iTempNext, jTempNext, board);
printf("%d %d\n", iTempNext, jTempNext);
if(board[iTempNext][jTempNext] == 0){
cell tempCell;
tempCell.row = iTempNext;
tempCell.col = jTempNext;
temp = (cell*)realloc(temp, sizeof(cell));
*(temp+cnt) = tempCell;
cnt++;
}
}
}
return temp;
}
int** board : 2d 数组,我动态分配它并将该数组的所有元素初始化为 0。我在初始化后打印了 2d 数组。
问题是在第 2 个 for 循环之后,该程序在访问二维数组时出现分段错误。我无法在第 3 个 for 循环期间访问 2d 数组元素。
Initializing Complete...
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Enter the start position (i, j): 3 3
iTempNext jTempNext
1 4
2 5
4 5
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400b1c in cellList (i=0x7fffffffe5bc, j=0x7fffffffe5c0, board=0x603010) at main.c:144
144 if(board[iTempNext][jTempNext] == 0){
这是使用gdb后的结果。我该如何解决这个问题
这一行:
temp = (cell*)realloc(temp, sizeof(cell));
调用未定义的行为,因为temp
未初始化。你应该初始化它,例如 cell *temp = NULL;
(理解 realloc
可以接受一个 NULL
指针,在这种情况下它等同于 malloc
,或者以前的 malloced/calloced/realloced
指针)。
并且 不要从 malloc 转换 return。这是 C,不是 C++。搜索此站点以了解为什么强制转换 malloc
return 值在 C 中不受欢迎。
我学过数据结构和算法,我遇到了动态二维数组的问题。这是我的代码的一部分。编码问题是骑士之旅
int iMove[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int jMove[8] = { 1, 2, 2, 1,-1, -2, -2, -1};
cell* cellList(int* i, int* j, int** board){
int k;
cell* temp;
int iTempNext; int jTempNext;
int maxSampleNum = 8;
int cnt = 0;
int val;
for(k = 0; k < maxSampleNum; k++){
iTempNext = (*i) + iMove[k];
jTempNext = (*j) + jMove[k];
//1. get list 0<=i<=7 && 0<=j<=7
if( (0 <= iTempNext && iTempNext <= 7) && (0 <= jTempNext && jTempNext <= 7)){
//2. get the 0 value cells
//val = canMove(iTempNext, jTempNext, board);
printf("%d %d\n", iTempNext, jTempNext);
if(board[iTempNext][jTempNext] == 0){
cell tempCell;
tempCell.row = iTempNext;
tempCell.col = jTempNext;
temp = (cell*)realloc(temp, sizeof(cell));
*(temp+cnt) = tempCell;
cnt++;
}
}
}
return temp;
}
int** board : 2d 数组,我动态分配它并将该数组的所有元素初始化为 0。我在初始化后打印了 2d 数组。 问题是在第 2 个 for 循环之后,该程序在访问二维数组时出现分段错误。我无法在第 3 个 for 循环期间访问 2d 数组元素。
Initializing Complete...
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Enter the start position (i, j): 3 3
iTempNext jTempNext
1 4
2 5
4 5
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400b1c in cellList (i=0x7fffffffe5bc, j=0x7fffffffe5c0, board=0x603010) at main.c:144
144 if(board[iTempNext][jTempNext] == 0){
这是使用gdb后的结果。我该如何解决这个问题
这一行:
temp = (cell*)realloc(temp, sizeof(cell));
调用未定义的行为,因为temp
未初始化。你应该初始化它,例如 cell *temp = NULL;
(理解 realloc
可以接受一个 NULL
指针,在这种情况下它等同于 malloc
,或者以前的 malloced/calloced/realloced
指针)。
并且 不要从 malloc 转换 return。这是 C,不是 C++。搜索此站点以了解为什么强制转换 malloc
return 值在 C 中不受欢迎。