11

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Luron
  • 1,157
  • 5
  • 17
  • 30
  • 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. – Steve Townsend Oct 04 '10 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. – shuttle87 Oct 04 '10 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) – jalf Oct 04 '10 at 17:43
  • alright relax everyone. this was for a friend. – Luron Oct 04 '10 at 19:51
  • 4
    GMan, what is your _problem?_ – Andres Jaan Tack Oct 04 '10 at 20:05
  • alright everyone needs to relax. it was a favor for my friend we had 5 books open and i didnt feel like spending my time on him if any of you couldve helped him in a minute. which occured. so everyone chill out. – Luron Oct 05 '10 at 16:07
  • @Luron - please accept some other answer, I'd like to delete mine. – Kiril Kirov Feb 26 '14 at 08:33

5 Answers5

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
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".

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
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.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • 1
    THANK YOU! thats exactly what my friend neeeded! – Luron Oct 04 '10 at 16:41
  • @pts - thanks for the edid! I wondered how can I do that (I didn't see that in FAQ), but now I know. 10x again (: – Kiril Kirov Oct 04 '10 at 16:43
  • 14
    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. – Sjoerd Oct 04 '10 at 16:56
  • 7
    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). – Martin York Oct 04 '10 at 17:13
  • @Luron: What did your friend try besides comparing the pointer to NULL? This is bizarre. Also, Sjoerd is right. If you in fact have an array created with array syntax, it (almost) cannot be NULL. – Potatoswatter Oct 04 '10 at 17:15
  • 3
    This check makes absolutely no sense for `a` declared with array type. Array is an object. Objects cannot reside at null address, by definition. – AnT stands with Russia Oct 04 '10 at 17:25
  • @Andrey: The address of an array is the address of the first object, and `new[]` returns the address and type of the first object anyway, not array type. See Sjoerd's comment. – Potatoswatter Oct 04 '10 at 17:46
  • @ SZjoerd - I know that (about nothrow), but it's true, that this ca happen. I had to tell him, why not? It's good to know.. It's always good. @Martin York - it depends on what you've used to see. It's safer, that's why I use it. @Potatoswatter - yeah, I agree, but "almost" does **not** mean "never" – Kiril Kirov Oct 04 '10 at 17:47
  • 2
    @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. – AnT stands with Russia Oct 04 '10 at 17:55
  • @Andrey: OK, you're referring to Kiril's code specifically and not the question. Sorry. – Potatoswatter Oct 04 '10 at 18:02
  • 1
    @kill Kirov: Its NOT safer by any measure. I am used to reading left to right (like most Europeans) so naturally the test tends to come on the right. But I am OK with others putting it on the left whatever their motives. – Martin York Oct 04 '10 at 18:03
  • @Martin: [off-topic] I could of sworn your SO profile said Seattle...a move? – GManNickG Oct 04 '10 at 18:27
  • @Martin: yes,probably 'safer' is not the word,but.. What i mean is avoiding situations like( for example ) if( xSomeVar = xSomeConst ) { /* .. */ } Instead of if( xSomeVar == xSomeConst ) /* .. */ Which,sometimes, happens..when you've been writting code for hours.. (: – Kiril Kirov Oct 04 '10 at 19:01
  • 1
    @Kiril Kirov: That situation is avoided by setting your warnings to the highest level possible (which you should have done already as 99% of warnings are actually real problems that either must be fixed (or at least explicitly documented and manually turned off for that situation)). Also your compiler should be set to treat all warnings as errors (as noted above they are usually errors). So that situation will never happen. – Martin York Oct 04 '10 at 19:34
  • 1
    As noted above I do not have a problem with this style (style is a personal preference and whatever makes you comfortable), I do have a problem with people using it for the wrong reasons (to try and protect themselves from errors). It gives a false sense of security as it is easy to slip up and put the NULL on the right (then you have no protection). Thus you are getting a false sense of security; alternatively using the available compiler flags actually gets you the required security and detects/prevent the assignment. – Martin York Oct 04 '10 at 19:39
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)
Vintharas
  • 190
  • 1
  • 8
  • 1
    The default version of new will never return NULL (although the nothrow version can). – Sjoerd Oct 04 '10 at 16:58
  • 1
    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. – David Thornley Oct 04 '10 at 17:16
  • I agree with you David. The former declaration was only for illustrating the dynamic allocation behavior in opposition to the first automatic allocation. – Vintharas Oct 04 '10 at 17:52
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.

pts
  • 80,836
  • 20
  • 110
  • 183