I have a problem with, i think, function INPUT. But i can't understand how to fix it [functions code]
typedef struct Item {
int data;
struct Item* next;
} Item;
typedef struct Queue{
Item* head;
Item* tail;
} Queue;
void input(Queue* list,int* stroka) {
Item* new = (Item*)malloc(sizeof(Item));
new->data = *stroka;
new->next = NULL;
if (!list->head) {
list->head = new;
list->tail = new;
} else {
list->tail->next = new;
list->tail = new;
}
}
int main() {
int stroka;
Queue* list = (Queue*)malloc(sizeof(Queue));
while(stroka != EOF) {
stroka = getchar();
input(list, &stroka);
}
printf("\n");
print(list);
}
[valgrind message]
Conditional jump or move depends on uninitialised value(s)
==29969== at 0x10931A: input ()
==29969== by 0x1093CF: main ()
==29969== Uninitialised value was created by a heap allocation
==29969== at 0x48427B5: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==29969== by 0x1093B1: main ()
tried to change some blocks of code but i didn't come to anything
while(stroka != EOF)What isstrokasupposed to contain when you first come here? Does your compiler mention something about usingstrokawithout assigning any value first? You probably wanted to usedo {} while ()instead ofwhile(){}.inputfunction? Why not just provide the int value itself?Queue(which seems unnecessary) and theItems, and you do not free them before the program exits. Valgrind will report this, as memory that is unfreed but still reachable. Strictly speaking, that's not a leak, and not a significant problem. But if you need to attain a completely clean report from Valgrind then free those objects before terminating.