以下代码是迷宫问题程序。我可以成功地获得路径的方向。但是,当我使用valgrind检查我的代码时,会出现一些问题。
001: #include <stdio.h>
002: #include <stdlib.h>
003: #include <string.h>
004:
005: struct each_path {
006: int step_nums, coin_nums;
007: int step_assume_num;
008: // char **step_dir;
009: char step_dir[100][100];
010: };
011:
012: void initialized_params();
013: void *readFile(char *fileName);
014: void maze_1Dto2D(char *array);
015: void visit(int, int);
016: void display_direction();
017: struct each_path epath[100];
018: int path_assume_num;
019: char maze1D[5000], maze2D[1000][1000], maze2D_tmp[1000][1000];
020: int maze_height, maze_width;
021: int startI = 1, startJ = 1, endI, endJ; // entrance & exit
022: int path_nums, coin_nums, min_step_num, min_path_num;
023:
024: int main(void) {
025: int i;
026:
027: initialized_params();
028: // read map
029: readFile("maze1.txt");
030: for ( i = 0; i < maze_height; ++i ) {
031: memcpy(maze2D_tmp[i], maze2D[i], maze_width + 1);
032: }
033:
034: endI = maze_height - 2;
035: endJ = maze_width - 2;
036: visit(startI, startJ);
037:
038: exit(0);
039: }
078: void maze_1Dto2D(char *array) {
079: size_t i = 0, j = 0, num = 0;
080:
081: for ( i = 0; i < maze_height ; ++i)
082: {
083: for ( j = 0; j < maze_width + 1; ++j, ++num)
084: {
085: maze2D[i][j] = array[num];
086:
087: if (array[num] == '\r')
088: --j;
089: else if (array[num] == '\n')
090: maze2D[i][j] = '\0';
091: else
092: maze2D[i][j] = array[num];
093: }
094: }
095: }
096:
097: void visit(int i, int j) {
098: int preI, preJ, curI = 1, curJ = 1;
099: int step_nums = 0, step_assume_num = 10; // entrance is not included
100: char dir[6];
101: int m, n;
102:
103: if (maze2D_tmp[i][j] == '2')
104: coin_nums++;
105: maze2D_tmp[i][j] = '3';
106:
107: if (i == endI && j == endJ) {
108: if (path_nums >= path_assume_num) {
109: path_assume_num *= 2;
110:
111: epath[path_nums].step_assume_num = step_assume_num;
112: }
113: while (curI != endI || curJ != endJ)
114: {
115:
116: if ( maze2D_tmp[curI][curJ + 1] == '3' && preJ != (curJ + 1) ) {
117: preI = curI;
118: preJ = curJ;
119: curJ++;
120: strcpy(dir, "right");
121:
122: }
123: else if ( maze2D_tmp[curI + 1][curJ] == '3' && preI != (curI + 1) ) {
124: preI = curI;
125: preJ = curJ;
126: curI++;
127: strcpy(dir, "down");
128: }
129: else if ( maze2D_tmp[curI - 1][curJ] == '3' && preI != (curI - 1) ) {
130: preI = curI;
131: preJ = curJ;
132: curI--;
133: strcpy(dir, "up");
134: }
135: else if ( maze2D_tmp[curI][curJ - 1] == '3' && preJ != (curJ - 1)) {
136: preI = curI;
137: preJ = curJ;
138: curJ--;
139: strcpy(dir, "left");
140: }
141: strcpy(epath[path_nums].step_dir[step_nums], dir);
142: step_nums++;
143: }
144: epath[path_nums].step_nums = step_nums;
145: epath[path_nums].coin_nums = coin_nums;
146: path_nums++;
147: if (step_nums < min_step_num)
148: {
149: min_step_num = step_nums;
150: min_path_num = path_nums;
151: }
152: }
153:
154: if (maze2D_tmp[i][j + 1] == '1' || maze2D_tmp[i][j + 1] == '2') visit(i, j + 1);
155: if (maze2D_tmp[i + 1][j] == '1' || maze2D_tmp[i + 1][j] == '2') visit(i + 1, j);
156: if (maze2D_tmp[i][j - 1] == '1' || maze2D_tmp[i][j - 1] == '2') visit(i, j - 1);
157: if (maze2D_tmp[i - 1][j] == '1' || maze2D_tmp[i - 1][j] == '2') visit(i - 1, j);
158:
159: if (maze2D[i][j] == '2')
160: {
161: maze2D_tmp[i][j] = '2';
162: coin_nums--;
163: }
164: else
165: maze2D_tmp[i][j] = '1';
166: }
我想问为什么我从Valgrind那里得到以下两句话! &#34;条件跳转或移动取决于未初始化的值&#34; &#34;未初始化的值是由堆栈分配&#34;
创建的==25816== Conditional jump or move depends on uninitialised value(s)
==25816== at 0x400B4B: visit (mazeproblemA.c:123)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D9A: visit (mazeproblemA.c:155)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D3E: visit (mazeproblemA.c:154)
==25816== by 0x400D9A: visit (mazeproblemA.c:155)
==25816== by 0x400D9A: visit (mazeproblemA.c:155)
==25816== by 0x400DF6: visit (mazeproblemA.c:156)
==25816== Uninitialised value was created by a stack allocation
==25816== at 0x4009E3: visit (mazeproblemA.c:97)
答案 0 :(得分:1)
当您首次在while
函数中输入visit
循环时,变量preI
和preJ
尚未初始化。 if
中的while
语句然后读取那些未初始化的值。那是valgrind所抱怨的。
您需要为这些变量赋予有意义的初始值,可能为0。