Reputation: 622
I have a problem with OPT algorithm as valgrind is giving me error (Conditional jump or move depends on uninitialised value(s)) at line #87. I guess it has something to do with "break", is there a way to fix this?
#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 );
}
}
}
Upvotes: 0
Views: 2062
Reputation: 3147
The for loop at line 85 looks strange. You are incrementing s, and continuing as long as it stays greater than 0. But i doesn't look negative to me, so s would always be greater than 0. Also, i doesn't look correctly initialized, as it is just set to len after the loop at line 71. Is that by design?
Basically, it looks as though you are comparing with buff[s], but you are incrementing s more than what you may have done when initializing buff.
Upvotes: 1
Reputation: 1499
I think you have to initialize all your variables, as "int j, s, k, tmp, available = 1, max = 0, pos, pf = 0;" because you are using all those variables inside conditionals..
EDIT: it is also a good practice...
Upvotes: 0
Reputation: 24433
Seems like you need to initialize all elements of your buff[80]
and frameset[len]
to something.. maybe 0
Also you also need to ensure that len is not greater than 80
otherwise you will be reading outside of an array.
if(frameset[j] == buff[s]) {
flag[j] = s;
break;
}
Upvotes: 1