The following code is a maze problem program. I can get the direction of the path successfully. However, there're some problems happening when I use valgrind to check my code.
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: }
I want to ask why I get the following two sentences from Valgrind! "Conditional jump or move depends on uninitialised value(s)" "Uninitialised value was created by a stack allocation"
==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)
When you first enter the while
loop in your visit
function, the variables preI
and preJ
have not yet been initialized. The if
statements inside the while
then read those uninitialized values. That's what valgrind is complaining about.
You need to give these variables initial values that make sense, perhaps 0.