Vector: deque iterator not dereferencable  
Author Message
LeonR





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

VC++ 8.0:

typedef vector<mytype ...> myvector;

typedef CComObject<CComEnum<...>> myenum;

myvector vect;

myenum enum;

...

mytype* p0 = &(*vect.begin());

mytype* p1 = &(*vect.end()); ->error: ->"vector iterator not dereferencable"

well...

mytype* p1 = &(*(--vect.end())); -> ok, but

enum.init(p0, p1...) ->lost last element of vect

mytype* p1 = &(*(--vect.end()));

p1++;

enum.init(p0, p1...) ->ok

Your comments, please



Visual C++13  
 
 
Brian Kramer





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

Your question should be in a separate thread (I just split it)  Also, it's not clear what you are asking.
 
 
Viorel.





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

Maybe you should use front and back functions in order to obtain the first and the last item of the vector

 

mytype * p0 = &vect.front();

mytype * p1 = &vect.back();


 
 
Martin Richter





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

mytype* p1 = &(*vect.end()); ->error: ->"vector iterator not dereferencable"

This says that this element is not valid and does not point to a valid object, the end iterator points behind the last valid element. As written before use front and back, but take care that also this objects are only valid when size()!=0



 
 
kuphryn





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

Correct.

operator []

Kuphryn


 
 
Neo_ms





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

Hi,

iterator at object.end() is not referencable, it is additional node in vector that marks the end. So it doesn't sore any data. It you write for eg: for(vector<T>::iterator i = collection.begin(); i != collection.end(); i++) {... } then the loop will go through all the elem-s in the vector. If you want to acces the last element through the iterator you should use rbeging(), which is reverse iterator. If not, you can always write sth like this: do_something(a.begin, --a.end()); if do_something has of course conditional that it ends on that iterator, however it isn't common. If you aren't very primar in C++ then I suggest you can use some of functions like: for_each(interator...); it is in <functional> (or algorythm:P).



 
 
LeonR





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

yes - for new thread "what we actually want for actually safety from VC++ 8" :)
 
 
LeonR





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

brilliant :)

I just would like to point attention of VC team on such node(s) in such checks. I understand why this check was introduced, but I guess it should take into account “additional nodes”.


 
 
Martin Richter





PostPosted: Visual C++ Language, Vector: deque iterator not dereferencable Top

Read this links about checked iterators

http://msdn2.microsoft.com/en-us/library/y9ww7c1a.aspx

and debug iterator support:

http://msdn2.microsoft.com/en-us/library/h23zsaxy.aspx