views:

328

answers:

6

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

+11  A: 

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
+1  A: 

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).

Kiril Kirov
THANK YOU! thats exactly what my friend neeeded!
Luron
@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
@Luron - you're welcome(: I'm glad that I helped. BR, Kiril
Kiril Kirov
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
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
@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
This check makes absolutely no sense for `a` declared with array type. Array is an object. Objects cannot reside at null address, by definition.
AndreyT
@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
@ 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
@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.
AndreyT
@Andrey: OK, you're referring to Kiril's code specifically and not the question. Sorry.
Potatoswatter
@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
@Martin: [off-topic] I could of sworn your SO profile said Seattle...a move?
GMan
@Martin: [off-topic] Ah, tricky tricky.
GMan
@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
@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
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
+9  A: 

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".

AndreyT
A: 

Answer to your new question:

An array is only a pointer to the first element of the array. The following elements are stored after the first one. So this means in C++, you have no "arrays" like you know them from Java. It is only a pointer. And a pointer has a size depending on your architecture (x64 or x86).

This means you have two possible solutions:

  • Make use of the vector class, which keeps the array size for you.
  • Let join an integer the usage of arrays everywhere in C++, which represents the size.

Answer to your previous question:

So this means you have to check if the pointer to the first element is null.

int myIntArray[30];  // Create an int-array on the stack.
if (myIntArray == 0)
{
    printf("I'm crazy!!! This code will never be executed");
}
or
int *myIntArray = new myIntArray[30]; // Create array on the heap
if (myIntArray == 0)
{
    // Same as here above
}
But of course you can have a null-pointer:
int *myPointerToInt = 0;
if (myPointerToInt == 0)
{
   // Do whatever you want
}
Martijn Courteaux
No, no, no: "An array is only a pointer to the first element of the array".
Oli Charlesworth
Arrays are not pointers. You need to forget you think you know they are.
GMan
+2  A: 

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
The default version of new will never return NULL (although the nothrow version can).
Sjoerd
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
I agree with you David. The former declaration was only for illustrating the dynamic allocation behavior in opposition to the first automatic allocation.
Vintharas
A: 

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