Mark A
Mark A

Reputation: 39

Check if whole array is empty

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.

Upvotes: 0

Views: 1888

Answers (3)

Omer
Omer

Reputation: 704

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]))
        {
            // ....
        }
    }

Upvotes: 0

FinalMarine255
FinalMarine255

Reputation: 11

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.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

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] = {};

Upvotes: 1

Related Questions