2x2魔方C语言程序栈溢出问题求助:顶层旋转功能异常
问题:2x2魔方C语言程序栈溢出(Stack Smashing)错误
我近期尝试编写一个可实现2x2魔方6种基础顺时针90°操作的C语言程序,在实现顶层90°旋转功能时,理论逻辑已梳理完成,但编译运行时GCC报「Stack Smashing」错误,调整内存分配后仍未解决,附上代码求助定位修复。
原代码:
#include <stdio.h> #include <stdlib.h> typedef char rubik_t[6][2][2]; /* Rotation in General: top1 top2 left1 left2 90° Turn left2 sideTopL sideTopR right1 --------------------> bottom2 sideBotL sideTopL top1 left1 sideBotL sideBotR right2 --------------------> bottom1 sideBotR sideTopR top2 bottom2 bottom1 right2 right1 */ //Copy the Input Cube into a new Array char *copyRubik(rubik_t rubik, rubik_t *copied){ int i, j, k; for(i = 0; i < 6; i++){ for(j = 0; j < 2; j++){ for(k = 0; k < 2; k++){ *copied[i][j][k] = rubik[i][j][k]; } } } return 0; } //Function to turn the Top of the Cube void turnTop(rubik_t *copied){ char temp1, temp2, temp3, temp4, temp5; char *left1 = copied[0][0][1]; char *left2 = copied[0][1][1]; char *right1 = copied[2][1][1]; char *right2 = copied[2][0][1]; char *top1 = copied[3][0][1]; char *top2 = copied[3][1][1]; char *bottom1 = copied[1][1][1]; char *bottom2 = copied[1][0][1]; char *sideTopL = copied[4][0][1]; char *sideBotL = copied[4][0][0]; char *sideTopR = copied[4][1][1]; char *sideBotR = copied[4][1][0]; temp1 = *top1; temp2 = *top2; temp3 = *sideBotL; temp4 = *sideTopL; temp5 = *sideTopR; *left1 = *bottom1; *left2 = *bottom2; *sideBotL = *sideBotR; *right1 = temp1; *right2 = temp2; *sideTopL = temp3; *sideTopR = temp4; *sideBotR = temp5; *top1 = *left1; *top2 = *left2; *bottom1 = *right1; *bottom2 = *right2; } int main(){ //Basic (solved) Rubik's cube rubik_t cube_ordered = { {{'B','B'},{'B','B'}}, {{'O','O'},{'O','O'}}, {{'G','G'},{'G','G'}}, {{'R','R'},{'R','R'}}, {{'W','W'},{'W','W'}}, {{'Y','Y'},{'Y','Y'}} }; rubik_t copiedCube; char *copied = malloc(25); copyRubik(cube_ordered, &copiedCube); turnTop(&copiedCube); // Print the Cube int i, j, k; for(i = 0; i < 6; i++){ for(j = 0; j < 2; j++){ for(k = 0; k < 2; k++){ printf("%c", copiedCube[i][j][k]); if(k == 1){ printf(" "); if(j == 1){ printf("\n"); if(i == 5){ printf("\n"); } } } } } } free(copied); return 0; }
问题定位与修复说明
1. 指针优先级错误(栈溢出核心原因)
copyRubik函数中,*copied[i][j][k]的运算符优先级错误:数组下标[]优先级高于解引用*,实际会被解析为*(copied[i][j][k]),但copied是指向单个rubik_t的指针,这样会导致指针越界访问栈内存,触发Stack Smashing。正确写法应为(*copied)[i][j][k],先解引用得到整个魔方数组,再访问对应元素。turnTop函数中,同样存在指针访问错误:copied[0][0][1]错误地将copied当作数组指针处理,正确写法是(*copied)[0][0][1],直接访问指向的魔方数组元素。
2. 冗余动态内存分配
main函数中char *copied = malloc(25);完全无用,既未参与魔方操作也未被使用,直接删除即可,避免不必要的内存管理操作。
3. 旋转逻辑错误
原turnTop的赋值顺序混乱,未按照顺时针90°旋转的正确逻辑保存和移动色块,修复后调整了临时变量的使用和赋值顺序,确保色块按预期移动。
修复后的完整代码
#include <stdio.h> #include <stdlib.h> typedef char rubik_t[6][2][2]; /* Rotation in General: top1 top2 left1 left2 90° Turn left2 sideTopL sideTopR right1 --------------------> bottom2 sideBotL sideTopL top1 left1 sideBotL sideBotR right2 --------------------> bottom1 sideBotR sideTopR top2 bottom2 bottom1 right2 right1 */ //Copy the Input Cube into a new Array void copyRubik(rubik_t rubik, rubik_t *copied){ int i, j, k; for(i = 0; i < 6; i++){ for(j = 0; j < 2; j++){ for(k = 0; k < 2; k++){ (*copied)[i][j][k] = rubik[i][j][k]; } } } } //Function to turn the Top of the Cube void turnTop(rubik_t *copied){ // 先保存顶面原始状态(顶面是索引4的面) char top_side[2][2]; for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ top_side[i][j] = (*copied)[4][i][j]; } } // 顺时针旋转顶面自身90° (*copied)[4][0][0] = top_side[1][0]; (*copied)[4][0][1] = top_side[0][0]; (*copied)[4][1][1] = top_side[0][1]; (*copied)[4][1][0] = top_side[1][1]; // 保存周围四个面的顶部边缘 char temp_left = (*copied)[0][0][1]; char temp_front = (*copied)[3][0][1]; char temp_right = (*copied)[2][0][1]; char temp_back = (*copied)[1][0][1]; // 周围面顶部边缘顺时针移动 (*copied)[0][0][1] = temp_back; (*copied)[3][0][1] = temp_left; (*copied)[2][0][1] = temp_front; (*copied)[1][0][1] = temp_right; // 保存周围四个面的第二行边缘 temp_left = (*copied)[0][1][1]; temp_front = (*copied)[3][1][1]; temp_right = (*copied)[2][1][1]; temp_back = (*copied)[1][1][1]; // 周围面第二行边缘顺时针移动 (*copied)[0][1][1] = temp_back; (*copied)[3][1][1] = temp_left; (*copied)[2][1][1] = temp_front; (*copied)[1][1][1] = temp_right; } int main(){ //Basic (solved) Rubik's cube rubik_t cube_ordered = { {{'B','B'},{'B','B'}}, // 左 {{'O','O'},{'O','O'}}, // 后 {{'G','G'},{'G','G'}}, // 右 {{'R','R'},{'R','R'}}, // 前 {{'W','W'},{'W','W'}}, // 顶 {{'Y','Y'},{'Y','Y'}} // 底 }; rubik_t copiedCube; copyRubik(cube_ordered, &copiedCube); turnTop(&copiedCube); // Print the Cube int i, j, k; for(i = 0; i < 6; i++){ printf("Face %d:\n", i); for(j = 0; j < 2; j++){ for(k = 0; k < 2; k++){ printf("%c ", copiedCube[i][j][k]); } printf("\n"); } printf("\n"); } return 0; }
内容的提问来源于stack exchange,提问作者Jeremy Gruhnert
相关产品推荐
相关产品推荐

