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
}
2
  • 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? Commented Jan 15, 2019 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. Commented Jan 15, 2019 at 11:35

1 Answer 1

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

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

2 Comments

Thank you, I think I am going to create big block.
@mosambers Excellent :) Welcome to the "efficient data storage" club!

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.