valgrindがエラーを返すため、OPTアルゴリズムに問題があります(条件付きジャンプまたは移動は初期化されていない値に依存します) 87。私はそれが "休憩"と関係があると思います、これを修正する方法はありますか?C - Valgrindと "条件付きジャンプまたは移動が初期化されていない値に依存する"エラー
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int convert_one_char(const char c) {
if(c == '0') return 0;
else if(c == '1') return 1;
else if(c == '2') return 2;
else if(c == '3') return 3;
else if(c == '4') return 4;
else if(c == '5') return 5;
else if(c == '6') return 6;
else if(c == '7') return 7;
else if(c == '8') return 8;
else if(c == '9') return 9;
else return -1;
}
void print(int n[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d", n[i]);
if (i != size-1) {
printf(" | ");
} else {
printf(": ");
}
}
}
int main (int argc, char *argv[]) {
/* argc should be 3 */
if (argc != 3) {
printf("Napaka: Potrebna sta 2 argumenta!\n");
} else {
FILE *file = freopen(argv[2], "r", stdin);
if (file == 0) {
printf("Napaka: 404!\n");
} else {
int num_count = 0, len = atoi(argv[1]), buff[80], i = 0;
char c;
/* read file contents */
do {
c = getchar();
if (i%2 == 0 && isdigit(c)) {
buff[num_count] = convert_one_char(c);
num_count++;
}
i++;
} while (c != EOF);
/* print parsed file */
printf("Referencni niz: ");
for (i = 0; i < num_count; i++) printf("%d ", buff[i]);
printf("\n");
/* opt algorithm */
int frameset[len];
int flag[len];
for (i = 0; i < len; i++) {
frameset[i] = -1;
flag[i] = 0;
}
int j, s, k, tmp, available = 1, max = 0, pos, pf = 0;
for (i = 0; i < len; i++) {
frameset[i] = buff[i];
print(frameset, len);
printf("Vstavim %d\n", buff[i]);
pf++;
}
while (i < num_count) {
max = 0;
for (k = 0; k < len; k++) if (frameset[k] == buff[i]) available = 0;
if(available == 1) {
for(j = 0; j < len; j++) {
for(s = i+1; s >= 0; s++) {
if(frameset[j] == buff[s]) {
flag[j] = s; // <<< line 87
break;
}
if(s >= num_count) {
flag[j] = 100;
break;
}
}
}
for(k = 0; k < len; k++) {
if (flag[k] > max) {
max = flag[k];
pos = k;
}
}
frameset[pos] = buff[i];
print(frameset, len);
printf("Vstavim %d\n", buff[i]);
pf++;
} else {
i++;
available = 1;
}
}
printf("\n\nStevilo napak: %d\n", pf);
fclose(file);
}
}
}
あなたのconvert_one_charはずっと簡単かもしれません。ちょうど 'c - '0' 'をしてください。それはある種のTDWTFコードです。 –
'convert_one_char()'を次のように圧縮することができます: 'int convert_one_char(char c){if(isdigit(c))はcを返します。それ以外の場合-1を返します。 } '。 –
行番号を記載したものを投稿するときは、その行に参照している行を示すコメントをコードに挿入する必要があります。 –