0

I am trying program a queue with arrays in C++.

I used this approach https://stackoverflow.com/a/936709/7104310 as shown below.

My question: How can I index the arrays to fill them?

In a normal 2d-array it would be arr[3][2] for example. But I do not know how to do this with pointers. The question hat not been answered in the Solution upon.

Thank you!

#include <iostream>

#define MAX_SIZE 3

using namespace std;


// ary[i][j] is then rewritten as

//arr[rear*capacity + front]

// Class for queue
class msg_queue
{
    char **arr;     // array to store queue elements
    int capacity;   // maximum capacity of the queue
    int front;      // front points to front element in the queue (if any)
    int rear;       // rear points to last element in the queue
    int count;      // current size of the queue

public:
    msg_queue(int size = MAX_SIZE, int slot_length = MAX_SIZE);     // constructor

    void dequeue();
    void enqueue(char x);
    char peek();
    int size();
    bool isEmpty();
    bool isFull();
};

// Constructor to initialize queue
msg_queue::msg_queue(int size, int slot_length)
{
    arr = new char*[size];
    for (int i = 0; i < size; ++i) {
        arr[i] = new char[slot_length];
    }

    capacity = size;
    front = 0;
    rear = -1;
    count = 0;
}

// Utility function to remove front element from the queue
void msg_queue::dequeue()
{
    // check for queue underflow
    if (isEmpty())
    {
        cout << "UnderFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }

    cout << "Removing " << arr[front] << '\n';

    front = (front + 1) % capacity;
    count--;
}

// Utility function to add an item to the queue
void msg_queue::enqueue(char item)
{
    // check for queue overflow
    if (isFull())
    {
        cout << "OverFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }

    cout << "Inserting " << item << '\n';

    rear = (rear + 1) % capacity;
    arr[rear] = item;  //ERROR HERE
    count++;
}

// Utility function to return front element in the queue
char msg_queue::peek()
{
    if (isEmpty())
    {
        cout << "UnderFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }
    return arr[front]; //ERROR HERE
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
mosambers
  • 21
  • 1
  • 7
  • You don't save the `slot_size` anywhere, how would you know how big each sub-array was? Perhaps rewrite to use `std::vector` instead? – Some programmer dude Jan 15 '19 at 11:22
  • My question is why you are using a two dimensional array? The question you referenced is about making 2D arrays. But the code with the error is using a one dimensional array `arr[rear] = item; //ERROR HERE`. I think you are confused, certainly the code is. I can't see any reason a queue would need a 2D array, you should drop the 2D array and just use a simple 1D array. – john Jan 15 '19 at 11:35

1 Answers1

2

Well, it's still arr[3][2].

Although arrays are not pointers, the way we use them is effectively using a pointer because of the way they work and the way their name decays.

x[y] is *(x+y), by definition.

That being said, I would recommend you drop the 2D dynamic allocation (which is poison for your cache) and create one big block of Width×Height chars instead. You can use a little bit of maths to provide 2D indexes over that data.

Also you forgot to free any of that memory. If you use a nice std::vector to implement my suggested 1D data scheme (or even if you hire a vector of vectors, but ew!) then it'll be destroyed for you. Of course if you could do that then you'd probably be using std::queue

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055