0

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

5
  • 2
    Its telling you that you never initialize the queue you allocated. Commented Jan 23, 2023 at 15:53
  • 1
    Welcome to SO. while(stroka != EOF) What is stroka supposed to contain when you first come here? Does your compiler mention something about using stroka without assigning any value first? You probably wanted to use do {} while () instead of while(){}. Commented Jan 23, 2023 at 15:54
  • Unrelated, but why do you provide a pointer to int to input function? Why not just provide the int value itself? Commented Jan 23, 2023 at 15:55
  • Queue* list = (Queue*)calloc(1,sizeof(Queue)); this works correctly, but there are still problems with leaks Commented Jan 23, 2023 at 16:01
  • "there are still problems with leaks" -- well, yes and no. You dynamically allocate your Queue (which seems unnecessary) and the Items, 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. Commented Jan 23, 2023 at 16:14

1 Answer 1

3

In the while loop you are using an uninitialized object of the type int and trying it to compare with a null pointer

int stroka;
Queue* list = (Queue*)malloc(sizeof(Queue));
while(stroka != EOF) {
    stroka = getchar();
    input(list, &stroka);
}

that does not make sense.

And there is no need to allocate dynamically an object of the type Queue.

Also instead of getchar it is much better to use scanf to enter integers instead of single characters that will be interpreted as integers including the new line character '\n'..

Also there is no any sense to pass the object stroke through a pointer to the function.

The function should be declared and defined the following way

int input( Queue* list, int data ) 
{
    Item *new_item = malloc( sizeof( *new_item ) );
    int success = new_item != NULL;

    if ( success )
    {
        new_item->data = data;
        new_item->next = NULL;

        if ( list->head == NULL ) 
        {
            list->head = new_item;
        } 
        else 
        {
            list->tail->next = new_item;
        }

        list->tail = new_item;
    }

    return success;
}

And in main you can write for example

  int main( void ) 
  {
    Queue list = { .head = NULL, .tail = NULL };

    for ( int data; scanf( "%d", &data ) == 1; )
    {
        input( &list, data );
    }

    printf("\n");
    print(list);
}

If you want to enter characters then declare the function like

int input( Queue* list, char data );

and in main write

    for ( char data; scanf( " %c", &data ) == 1; )
    {
        input( &list, data );
    }

Pay attention to the space in the format string in the call of scanf. It allows to skip white space characters.

And do not forget to write a function that will free all the allocated memory when the list will nit be required any more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.