user466534
user466534

Reputation:

queue structure gives strange error

here is queue implementation which gives me very unclear error

#include<iostream>
#include "bool.h"
#include "item.h"
#include "queue.h"

using namespace std;

void init_queue(queue *q){
    q->first=0;
    q->last=queuesize-1;
    q->count=0;

}
       void enqueue (queue *q,item_type x){
           if(q->count>=queuesize)
               cout<<" queue everflow occurs during enqueue "<<endl;
           else
           {
               q->last=(q->last+1)%queuesize;
               q->m[q->last]=x;
               q->count=q->count+1;

           }




       }

             int dequeue (queue *q)
             {
                 item_type x;
                 if(q->count<=0) cout<<"empthy queue "<<endl;
                 else
                 {
                     x=q->m[q->first];
                     q->first=(q->first+1)%queuesize;
                     q->count=q->count-1;
                 }
                 return (x);




                 }



             item_type headq(queue *q)
             {
                 return(q->m[q->first]);
             }
             int empthy(queue *q){


                 if (q->count<=0) return (TRUE);
                 else return (FALSE);
             }

 void print_queue(queue *q){
     int i;
     i=q->first;
     while(i!=q->last)
     {
         cout<<q->m[i];
         i=(i+1)%queuesize;
     }
     cout<<q->m[i]<<"  ";

 }
 int main(){
     queue *q;
     init_queue(q);
 int a;
 while(cin>>a){
     enqueue(q,a);




 }
 print_queue(q);


    return 0;
}

see also please queue header file

#define  queuesize 1000
#include "item.h"

typedef struct
{
int  m[queuesize+1];
  int first;
  int last;
  int count;
  }queue;

error is that (actualy) it is not error just warning when i compile,but when i run it says local variable q uninitialized,so which variable q?it is name of structure,so when i run init_queue method it should initialize yes?

Upvotes: 0

Views: 323

Answers (6)

Steve Jessop
Steve Jessop

Reputation: 279225

it is name of structure

No, q is a pointer-to-queue-object. In main, you haven't assigned any value to q, and you haven't even created a queue object that it could point to. So q is uninitialized.

You should do this:

queue q;          // an instance of queue
queue *qptr = &q; // a pointer to that instance
// now use qptr (or &q) in place of q in the rest of the main function.

Upvotes: 2

Jeff Foster
Jeff Foster

Reputation: 44706

The problem is in these two lines of code.

 queue *q;
 init_queue(q);

Inside init_queue you are assuming the q has some memory allocated to it by dereferencing it using the -> operator. But it is just a pointer to a random address in memory (since it hasn't been initialized).

q->first=0;
q->last=queuesize-1;
q->count=0;

To fix this you should add something along the lines of

queue *q = new queue();

However, since you are in C++ it might be worth building a class to encapsulate the memory allocation in the constructor and free the memory in the destructor.

Even better would be to follow rageshctech's suggestion and avoid using manual memory allocation at all and just use something allocated on the stack (e.g. without a pointer).

Upvotes: 3

moshbear
moshbear

Reputation: 3322

You never allocated memory for the queue.

Instead of pointers, make a class with a non-default constructor and use that instead.

Pointers are a C thing. You're coding in C++ :P

Upvotes: 1

aJ.
aJ.

Reputation: 35450

 queue *q;      
init_queue(q); 

You are not allocating the memory for q.

queue *q = new queue;

You can allocate the queue on stack as well: ( recommended !)

queue q;
   init_queue(&q); 

Upvotes: 2

Ragesh Chakkadath
Ragesh Chakkadath

Reputation: 1531

You must instantiate the queue object.

int main(){
 queue *q = new queue;
 init_queue(q);

or

use q as a stack variable and pass the address.

int main(){
 queue q;
 init_queue(&q);

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409166

I guess the warning is about the variable q in main? It's because you define it as a pointer, but do not allocate it. Either define it as not a pointer, and use &q in all function calls, or allocate memory for it:

queue *q = new queue;

And before the return 0 in main free the memory:

delete q;

Upvotes: 1

Related Questions