11

how do i do that? well i want to check if the array is empty

7
  • 2
    How is your array for checking declared? Whether it is 'empty' or not depends on what you are storing in it, and your criteria for an 'empty' entry. Commented Oct 4, 2010 at 16:34
  • 3
    Generally speaking it's good to avoid raw arrays in favour of safer container classes )like the STL) when making c++ code. Commented Oct 4, 2010 at 16:37
  • 1
    "array is null" and "array is empty" are two different things. (And generally, an array cannot be null, although it can decay into a pointer, and pointers can be null) Commented Oct 4, 2010 at 17:43
  • alright relax everyone. this was for a friend. Commented Oct 4, 2010 at 19:51
  • 4
    GMan, what is your problem? Commented Oct 4, 2010 at 20:05

5 Answers 5

21

An array in C++ cannot be null; only a pointer can be null.

To test whether a pointer is null, you simply test whether it compares equal to NULL or 0.

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

Comments

10

Array in C++ cannot be "empty". When you define an array object, you explicitly specify the exact size of the array. That array contains (and always will contain) that exact number of elements you specified in the definition. No more no less. It will never be "empty".

Comments

7

Actually, when you have an array a[SIZE], you can always check:

if( NULL == a )
{
/*...*/
}

But it's not necessary, unless you created a dynamic array (using operator new).

See the other answers, I won't delete it just because it's accepted now. If other answer is accepted, I'll delete this "answer".


EDIT (almost 4 years later :) )

As I get many down-votes for this, I'd like to clarify: I know this is useless and a will never be NULL, but it technically answers the question about the NULL part.

Yes, it does NOT mean, the array is empty, NOT at all. As @JamesMcNellis notes below, arrays cannot be NULL, only pointers.

It could only be useful for dynamically allocated arrays with initialized pointer before the allocation.

Anyway, I'll wait for accepting other answer and will delete mine.

15 Comments

THANK YOU! thats exactly what my friend neeeded!
An array created with int a[SIZE]; will never be NULL. An array created with int* a = new int[SIZE]; will never be NULL. Only when using the nothrow version of new, about which beginners shouldn't be told, the check could be useful.
Stylistically putting the NULL on the left was a trend that became popular in the late 90's, (and it is OK) but I still prefer putting it on the right because in MNSHO it reads easier with the NULL on the right (easier to read means easier to maintain) (Arguments about it being safer are rubbish as the compiler warns you about assignment and you should not have any warnings anyway).
This check makes absolutely no sense for a declared with array type. Array is an object. Objects cannot reside at null address, by definition.
@Potatoswatter: That exactly why I said, verbatim: if a is declared with array type, then the check makes no sense. What I said does not apply to dynamically allocated arrays, since in that case a won't have array type.
|
6

You can use either static or "dynamic" arrays. An static array would be something like the following:

int array[5];

That represents an static array of 5 integer elements. This kind of array cannot be null, it is an array of 5 undefined integers.

A "dynamic" array, on the other hand would be something like this:

int* array = new array[5];

In this case, the pointer-to-int is pointing to an array of 5 elements. This pointer could be null, and you would check this case with a simple if statement:

if (array == 0)

3 Comments

The default version of new will never return NULL (although the nothrow version can).
Although something like int * array = 0; /* stuff */ array = new array[5]; will spend time with array being empty. Of course, declaring a variable that can't be initialized meaningfully is a code smell.
I agree with you David. The former declaration was only for illustrating the dynamic allocation behavior in opposition to the first automatic allocation.
2

If you are using an STL vector or list, you can use the empty or the size method to check for emptiness:

std::vector<int> v;
if (v.empty()) cout << "empty\n";
if (v.size() == 0) cout << "empty\n";
std::list<int> l;
if (l.empty()) cout << "empty\n";
if (l.size() == 0) cout << "empty\n";

A regular C++ array (like int a[]') or pointer (likeint* a) doesn't know its size.

For arrays declared with size (like int a[42] as a local or global variable or class member), you can use sizeof(a) / sizeof(a[0]) to get the declared size (42 in the example), which will usually not be 0. Arrays you declare this way are never NULL.

Comments

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.