Check if C ++ array is Null

how to do it? well i want to check if array is free

+5
source share
5 answers

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

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

But this is not necessary if you have not created a dynamic array (using the new operator).

See other answers, I will not delete it just because it is accepted now. If another answer is accepted, I will delete this "answer".


EDIT (almost 4 years later :))

How I get a lot of votes for this, I would like to clarify: I know that it is useless, and awill never be NULL, but it technically answers the question about the part NULL.

, , , . @JamesMcNellis , NULL, .

.

, .

+5

++ ; .

, , , NULL 0.

+18

++ "". , . ( ) , . , . "".

+9

, "" . :

int array[5];

5 . , 5 undefined .

"" , , :

int* array = new array[5];

--int 5 . , if:

if (array == 0)
+3

STL vector list, empty size :

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

++ (, int a[]') or pointer (like int * a) .

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

0
source

All Articles