Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check iterator pointer in c++

Tags:

c++

I want to know if the following check is necessary:

std::list<int> myList;
.....
for (std::list<int>::iterator itr = myList.begin(); itr != myList.end(); ++itr)
{
   if (itr != NULL) // Is This Check Necessary?
   {
      // DO SOMTHING.
   }    
}

I have seen this check in some places and I'm wondering if that is a necessary check. Maybe this check is necessary if the list is an input to a function?

Thanks, Ofer.

like image 512
Ofer Avatar asked Jan 29 '26 07:01

Ofer


2 Answers

No, it is an unnecessary check. You might want to check *itr != nullptr, if the list held some kind of pointers.

like image 86
Alon Avatar answered Jan 30 '26 20:01

Alon


No, it's not necessary. I am not sure it's even valid.

like image 20
NPE Avatar answered Jan 30 '26 21:01

NPE