c++ - Is it safe to use iterator pointing beyond indexable collection in calculations? -


when have simple pointer can safely wrap array this:

p+=x;   // p pointer p can point beyond end of array if(p>=array+array_size) p-=array_size;    // forget potential multiple wrap 

can same safely iterator on indexable container, esp. deque?:

p+=x; if(p>=mydeque.end()) p-=mydeque.size(); // forget potential multiple wrap 

in other words: safe take calculations iterator pointing beyond collection?

you can p += x; long still points within array/container or 1 past end of array/container (§5.7):

if both pointer operand , result point elements of same array object, or 1 past last element of array object, evaluation shall not produce overflow; otherwise, behavior undefined.

the same true comparisons between pointers. must both pointing @ same array or 1 past end. cannot pointing further that.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -