Thread: Problem when concatenating two dynamic queues

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Problem when concatenating two dynamic queues

    I have a Queue *q1, *q2 in main() that I send off the a function to initialize that looks like this:
    Code:
    Queue *CreateQueue () {
       Queue *queue;
    
       queue->front = queue->rear = NULL;
    
       return queue;
    }
    Doesn't this create a brand new Queue, initialize it to NULL, then return it back to the calling function?

    But what happens is that when I manipulate one queue, the exact same operation is performed on the other queue, as if they were the same queue.

    From my other thread asking for queue help (http://cboard.cprogramming.com/showthread.php?t=57999), itsme86's queue initialization works fine because it creates QUEUE q1, q2; in main () before sending it off to be initialized:
    Code:
    void new_queue(QUEUE *q)
    {
      q->head = q->tail = NULL;
    }
    Why doesn't my method work?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Doesn't this create a brand new Queue, initialize it to NULL, then return it back to the calling function?
    No. You aren't allocating memory for the queue.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Doesn't calling the function to to create a new variable make it separate from one that was created in a previous call? What I'm doing is basically this:
    Code:
    main()
    {
       // .........
    
       Queue *q1 = CreateQueue(q1);
       Queue *q2 = CreateQueue(q2); //why isn't this new q2 separate from q1?
    }
    I then malloc when I append an item onto the queue.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    I have a Queue *q1, *q2 in main() that I send off the a function to initialize that looks like this:
    Code:
    Queue *CreateQueue () {
       Queue *queue;
    
       queue->front = queue->rear = NULL;
    
       return queue;
    }
    you see, queue is just a pointer, it points to some random location. Here, you try to dereference it w/ -> operator, this is not correct. this is why you should malloc before doing this in CreateQueue() or just pass a pointer to an already existing variable of that type.
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with grid problem (dynamic programming)
    By scp89 in forum C Programming
    Replies: 15
    Last Post: 05-07-2009, 12:16 PM
  2. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  3. Problem deleting dynamic array
    By pliang in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2005, 04:07 PM
  4. Please help! Dynamic binary tree problem
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-15-2005, 09:56 PM
  5. C++ color dynamic problem
    By Dakkon in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2002, 06:04 PM