What does this code mean in "vector"? (C ++)

I created a program and it uses vector.h #include, iterators, etc. But when I run the program, under certain circumstances (I'm still trying to figure out what it will be) I get an assertion error referencing line 98 of vector.h. I went to line 98 of vector.h and got this:

 #if _HAS_ITERATOR_DEBUGGING
        if (this->_Mycont == 0
            || _Myptr < ((_Myvec *)this->_Mycont)->_Myfirst
            || ((_Myvec *)this->_Mycont)->_Mylast <= _Myptr)
            {
            _DEBUG_ERROR("vector iterator not dereferencable");
            _SCL_SECURE_OUT_OF_RANGE;
            }

      

Can someone please tell me what this means and what in my program is causing this assertion?

NB: line 98 for writing is the one that starts with "_DEBUG_ERROR (" vect ... "

NB: This is code in my program that I BELIEVE resulted in an error, but I'm not entirely sure.

CODE:

for(aI = antiviral_data.begin(); aI < antiviral_data.end();)
    {
        for(vI = viral_data.begin(); vI < viral_data.end();)
        {
            if((*aI)->x == (*vI)->x && (*aI)->y == (*vI)->y)
            {
                vI = viral_data.erase(vI);
                aI = antiviral_data.erase(aI);
            }
            else
            {
                vI++;
            }
        }
        if((*aI)->x >= maxx || (*aI)->x < 0 || (*aI)->y >= maxy || (*aI)->y < 0)
        {
            aI = antiviral_data.erase(aI);
        }
        else
        {
            aI++;
        }
    }

      

+1


a source to share


5 answers


The runtime detects that you are dereferencing an iterator that comes before begin () or after end ().

Imagine if you remove the last element in the vector antiviral_data

on line 7:

aI = antiviral_data.erase(aI);

      

aI

is set to antiviral_data.end()

, and when you look for it on line 14:

if((*aI)->x >= maxx ...

      



and also on line 5:

if((*aI)->x == (*vI)->x

      

You are looking for an iterator out of bounds.

The fix is ​​to check that aI != antiviral_data.end()

after the erase call to make sure you haven't reached the end of the vector before continuing to use it.

+10


a source


You really want to look at STL algorithms like remove_if

, instead of doing it manually.



+7


a source


A small general comment: when testing an iterator for, end()

don't use " <

", only " !=

". So the first lines of your code should look like this:

for(aI = antiviral_data.begin(); aI != antiviral_data.end();)
{
  for(vI = viral_data.begin(); vI != viral_data.end();)
  {
    ...

      

However, as Josh already pointed out, your specific error is on line 7.

+5


a source


In addition to the accepted answer and clarification on slavy13's answer -
( EDIT - and as Josh mentioned, not directly related to this question - I'm leaving it here for reference).

The code (but not this code) sometimes suggests that you can remove elements from the vector and keep iterating. This is a false assumption: after removing an element from the vector, all other iterators following the deleted element become invalid — you can no longer assume they are correct, and "bad things" can happen if you keep using them.

The reason for this is because a vector actually stores information in the form of an array. When an item is removed, all subsequent items are copied down one cell. Iterators are not updated appropriately.

It is highly recommended that you consult the STL documentation when trying to do this sort of thing, because it is entirely possible that such code will work with a certain STL implementation by accident, but not with others.

+1


a source


erasing an element in the vector invalidates all iterators.

-2


a source







All Articles