0

I've got a 2D array that acts as a message queue with a each message comprised as a char[]

#define BUFFER_LENGTH 64    
#define QUEUE_LENGTH 100
char MessageQueue[QUEUE_LENGTH][BUFFER_LENGTH];

I want to check if the array is empty at a particular value for the first dimension only

for(int i = 0; i < sizeof(MessageQueue); i++)
{ 
    if(MessageQueue[i] == 0)
        MessageQueue[i][j] = ....; 
}

However it is not entering the if statement if it is empty.

4
  • Define "empty" ? Whether you like it or not, that thing occupies QUEUE_LENGTH * BUFFER_LENGTH chars, and MessageQueue[i] will never be 0 declared as it is. And sizeof(MessageQueue) is definitely not what you want to use for that loop top-end. Commented Mar 2, 2014 at 23:50
  • 1
    The value of your first dimension isn't going to be 0. Ever. You've defined it as another array, hence the 2D. Commented Mar 2, 2014 at 23:52
  • You would need to check that the whole dimension is 0 (e.g. in a loop, check that MessageQueue[i][0] through to MessageQueue[i][BUFFER_LENGTH-1] are all 0. Commented Mar 2, 2014 at 23:53
  • By empty, I mean if there is no values inside the second dimension array. So I would essentially have to check each individual element in the array using nested loops? Commented Mar 2, 2014 at 23:59

3 Answers 3

1

If I have correctly understood you you want to check that the array doesn contain messages. I suppose that message is stored as a string literal. You can do this by using standard algorithm std::all_of declared in header <algorithm>

For example

#include <iostream>
#include <algorithm>
#include <iterator>

//...

if ( std::all_of( std::begin( MessageQueue ), std::end( MessageQueue ),
                  []( const char *s ) { return ( *s == '\0' ); } ) )
{
   std::cout << "The Message queue is empty" << std::endl;
} 

Take into account that initially the array shall be zero-initialized. For example

char MessageQueue[QUEUE_LENGTH][BUFFER_LENGTH] = {};
Sign up to request clarification or add additional context in comments.

Comments

1

In order to check if a dimension is empty you would need to check every element within the dimension. You can use a simple for loop to accomplish this.

There is, however, a problem with the code that you have posted. In you for loop you use sizeof(MessageQueue) which will not return QUEUE_LENGTH but rather QUEUE_LENGTH multiplied by BUFFER_LENGTH which will result in you accessing memory that you have not allocated.

This following code will do what you wish.

//Note that sizeof(MessageQueue) has been changed to QUEUE_LENGTH.
for(int i = 0; i < QUEUE_LENGTH; i++)
{
    bool isEmpty = true;
    for(int j = 0; j < BUFFER_LENGTH; j++)
    {
        if(MessageQueue[i][j] != 0)
        {
            isEmpty = false;
            break;
        };
    };
    if(isEmpty)
    {
        //DO SOME WORK.

        //Exit the for-loop
        break;
    };
};

While the above code will check to see if a dimension is empty, it is unlikely that it will be if the array was just created. For this reason you will want to set every element of the array to 0 before doing anything with it. This can be done by placing the following for loop some where in you code that will be run before anything accesses the array.

for(int i = 0; i < QUEUE_LENGTH; i++)
{
    for(int j = 0; j < BUFFER_LENGTH; j++)
    {
        MessageQueue[i][j] = 0;
    };
};

I would suggest placing this for loop in a function such as void InitMessageQueue() and then simply calling that function in the initialization of the program.

Comments

0

The first dimension of the array is occupied with the second dimension, therefor it could not be equal to 0. The second dimension may contain garbage since it is uninitialized.

If you want to make sure that the array is empty, initialize it first:

for (int i = 0; i < QUEUE_LENGTH; i++)
    for (int j = 0; j < BUFFER_LENGTH; j++)
        MessageQueue[i][j] = 0;

Then use a helper function:

bool isEmpty(char * arr)
{
    bool toReturn = true;

    for (int i = 0; i < BUFFER_LENGTH && toReturn; i++)
        toReturn = toReturn && 0 == arr[i];

    return toReturn;
}

usage example:

for (int i = 0; i < QUEUE_LENGTH; i++)
    {
        if (isEmpty(MessageQueue[i]))
        {
            // ....
        }
    }

3 Comments

This statement that "The first dimension of the array is occupied with a pointer to the second dimension" is wrong. There are no pointers in arrays.
It is no matter. Arrays does not store any addresses. Array is simply one extent of memory
Thanks, this was exactly what I was looking for. Have accepted as answer.

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.