Thread: Queue in C showing odd behavior

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    6

    Queue in C showing odd behavior

    Hi guys! I'm working on a project in C that requires a few queues, so I started to write a basic linked list style data structure that I could treat as a queue. Once I got the node struct and push() functions written I started testing it to make sure I did my node pointer switching correctly. The queue seems to work fine for all my tests except the one that loops a few times getting user input and storing the input in the queue. Unlike the others, this one does not work at all, all the nodes get overwritten to be the same value. This confuses me as I have a test where it uses push() in a loop and works fine, so I suspect its something to do with how I'm getting/treating the input, or a silly error that I'm somehow missing (pretty new to C still).
    Heres the output when the 3 tests are run:
    Code:
    Repeated calls test:
        Current Queue:
        str=1
        Current Queue:
        str=2    str=1
        Current Queue:
        str=3    str=2    str=1
        Current Queue:
        str=4    str=3    str=2    str=1
    Loop test:
        Current Queue:
        str=a
        Current Queue:
        str=b    str=a
        Current Queue:
        str=c    str=b    str=a
        Current Queue:
        str=d    str=c    str=b    str=a
    Loop input test:
    >>
    1
    >>
        Current Queue:
        str=1
    
    2
    >>
        Current Queue:
        str=2
        str=2
    
    3
    >>
        Current Queue:
        str=3
        str=3
        str=3
    
    4
    >>
        Current Queue:
        str=4
        str=4
        str=4
        str=4
    And here is my full testing program:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct node{
        char * str;
        struct node * next;
    } node;
    
    //the head of the queue
    node * head;
    
    //push a new node to the top of the queue
    void push(char * str) {
       struct node * new_node =  (struct node*) malloc(sizeof(struct node));
       new_node->str = str;
       new_node->next = head;
       head = new_node;
    }
    
    //print the queue
    void print_q() {
        printf("\tCurrent Queue:\n");
          struct node *curr = head;
        while(curr){
               printf("\tstr=%s", curr->str);
               curr = curr->next;
       }
    }
    
    //test pushing input in a loop
    void test3(){
        printf("Loop input test:\n");
        int line_max = 1024; //max input that each line can be
        char buf[line_max]; //buffer to store input in
        printf(">>\n");
        char * str = fgets(buf, line_max, stdin); //get line of input
        int i;
        for(i = 0; i < 4; i++){
            printf(">>\n");
            push(str);
            print_q();
            printf("\n");
              str = fgets(buf, line_max, stdin);
        }
    }
        
    //test pushing in a loop    
    void test2(){
        printf("Loop test:\n");
        int i;
        for(i = 0; i < 4; i++){
            if(i == 0)
                push("a");
            if(i == 1)
                push("b");
            if(i == 2)
                push("c");
            if(i == 3)
                push("d");
            print_q();
            printf("\n");
        }
    }
    
    //test pushing without loop
    void test1(){
        printf("Repeated calls test:\n");
        push("1");
        print_q();
        printf("\n");
    
        push("2");
        print_q();
        printf("\n");
    
        push("3");
        print_q();
        printf("\n");
    
        push("4");
        print_q();
        printf("\n");
    }
    
    int main(int argc, char const *argv[])
    {
        test1();
        head = NULL;
        test2();
        head = NULL;
        test3();
    
        return 0;
    }
    Any help pointing me in the right direction would be much appreciated as I can't tell why the 3rd test with user input fails while the others pass
    Last edited by sliceofpi; 06-06-2017 at 05:52 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you only have space allocated for a single string, so of course you cannot store more than one distinct string in the queue when all you are doing is getting the pointer of each node to point to same storage.

    There are other problems too:
    • You are callously leaking memory.
    • You can only ever have one queue at a time because of your use of a global variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    6
    Thanks for the help, your answer made sense. Also In this case I only ever want 1 queue, so a global head is fine for me. This was just a test program because I was confused, so I didn't manage my memory as I would in the actual project, but thank you for pointing out the mem leaks anyways!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. Two forms showing at once
    By Tperry23 in forum C# Programming
    Replies: 0
    Last Post: 06-07-2005, 11:51 PM
  3. icon not showing
    By xlnk in forum Windows Programming
    Replies: 2
    Last Post: 02-27-2004, 06:36 PM
  4. Pic not showing up
    By Hunter2 in forum Game Programming
    Replies: 14
    Last Post: 11-13-2002, 01:19 PM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM

Tags for this Thread