Uninitialised value was created by a heap allocation
-
Valgrind beschwert sich, dass mein array nicht gesetzt wird, und ich bin langsam an verzweifeln, weil ich den Fehler einfach nicht finden kann!
Meine Methode sieht so aus, und was sie macht, sie guckt einfach ob zwei meiner Strukturen aufenainderfolgend sind, und wenn ja, dann wird eine neue Struktur mit den neuen Koordinaten generiert
typedef struct { int start; int length; } my_struct;
my_struct* filter_my_struct_list(my_struct *list_fst, int *total_num) { if (*total_num == 0) { fprintf(stderr, "Number of my_structs should not be zero! Please check the input!\n"); exit(EXIT_FAILURE); } my_struct *new_list; if (*total_num == 1) { new_list =malloc(sizeof(my_struct)* 1); my_struct f = {list_fst[0].start, list_fst[0].length}; new_list[0] = f; return new_list; } int fst, remain_i; new_list =malloc(sizeof(my_struct)* (*total_num)); remain_i = fst = 0; while (fst < (*total_num - 1)) { int fst_in_loop = fst; int nxt = fst_in_loop+1; BOOL terminate = FALSE; while (!terminate) { if (((list_fst[fst_in_loop].start + list_fst[fst_in_loop].length) == list_fst[nxt].start)) { fst_in_loop++; nxt++; } else terminate = TRUE; if (nxt == (*total_num)) { terminate = TRUE;} } if (fst_in_loop != fst) { int new_len = list_fst[fst_in_loop].start + list_fst[fst_in_loop].length - list_fst[fst].start; my_struct f = {list_fst[fst].start, new_len}; new_list[remain_i] = f; } else new_list[remain_i] = list_fst[fst]; remain_i++; fst = nxt; if (nxt == (*total_num - 1)) { if (fst == fst_in_loop) { int f_1; for (f_1 = fst; f_1 <= nxt; f_1++) { my_struct f = {list_fst[f_1].start, list_fst[f_1].length}; new_list[remain_i] = f; remain_i++; } } else { my_struct f = {list_fst[nxt].start, list_fst[nxt].length}; new_list[remain_i] = f; remain_i++; } } } *total_num = remain_i; return new_list; }
Der Fehler soll in Zeile 16 sein. Ich habe schon das (*total_num) mit einem konkretem wert (z.B. 100) ersetzt, aber der Fehler ist noch da.
Ich versteh nicht, wieso dieser Fehler auftritt, ich fange ab, ob (*total_num) 0 oder 1 ist, also müssen auf jeden Fall werte gespeichert werden!
Weil wenn keine aufeinanderfolgenden Einträge gefunden werden, wird einfach der gleiche gespeichert: (else teil in Zeile 43
-
Ersetze malloc durch calloc.
Für die Verwendung solcher Variablennamenint f_1;
gehörst du gesteinigt.