Assertion failed in vector iterator

G

Guest

Hi,

I'm trying to "port" a project from VC++ 2003 to VC++ 2005 (Express
Edition). This project contains the following code on several places (It is
not exactly this code but a generalization of what it does.):

vector<int> int_vector;
vector<int>::iterator i, j;

for (i = int_vector.begin (); i != int_vector.end (); i++)
{
for (j = int_vector.begin (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.erase (j);
}
}
}

I realize the reason for doing this is not obvious in the generalization
above but a bit more understandable in the actual context (although far from
"good code" I assume).

The result of this is that an assert fails with the following information:

File: c:\program files\microsoft visual studio 8\vc\include\vector
Line: 117
Expression: ("this->_Mycont != NULL", 0)

This is one of the ++-operators in vector and the assert occurs on the
"i++"-instruction in the code above. _SCL_SECURE_VALIDATE (this->_Mycont !=
NULL) seems to be the assert that fails.

I have read about "Debug iterator support" and "Checked iterators" here:
http://msdn2.microsoft.com/en-us/library/ms235424(VS.80).aspx. It seems to me
that the assert has to do with one of these. I also read that these can be
disabled by using the following:

#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0

I have put these the first thing in the cpp-file with my main-function. I
still seem to get the assert failure. My questions are:

1) What, exactly, is causing the assert failure?
2) Is it possible to revert to the VC++ 2003 behavior where there is no
failure?
3) Is my theory about "Debug iterator support" and "Checked iterators" a
dead end or am I just using the #DEFINE:s the wrong way?

I want to point out that I realize that the obvious solution is to rewrite
the offending parts of the code. However, I have not written the code in
question myself so at this point I do not want to do this. I just want to
recreate the behavior from VC++ 2003 to recreate a functioning program and
then take it from there.
 
G

Guest

vector said:
vector<int>::iterator i, j;

for (i = int_vector.begin (); i != int_vector.end (); i++)
{
for (j = int_vector.begin (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.erase (j);
}
}
}

This looks like dodgy code to me.
if i and j point to the same element in the vector, *j == *i
You then erase the element from the vector and try to go to the next element.
What are i and j pointing to at that point? what they were pointing to was
erased.

If you look at the place in the vecor header where the assert is generated,
you'll see that the iterator has been invalidated.

VC2005 is trying to say that you made a logic error.
When I have to do something like that I use while loops. that way you can
move i and j before erasing that element.

maybe you can revert back to the original behavior by defining _SECURE_SCL
and _HAS_ITERATOR_DEBUGGING to 0?
Be sure that you define them before the vector header, but after StdAfx.h.
if vector is included inside StdAfx.h, then you have to make the defines
inside StdAfx.h

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tom Widmer [VC++ MVP]

Johan said:
Hi,

I'm trying to "port" a project from VC++ 2003 to VC++ 2005 (Express
Edition). This project contains the following code on several places (It is
not exactly this code but a generalization of what it does.):

vector<int> int_vector;
vector<int>::iterator i, j;

for (i = int_vector.begin (); i != int_vector.end (); i++)
{
for (j = int_vector.begin (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.erase (j);
}
}
}

I realize the reason for doing this is not obvious in the generalization
above but a bit more understandable in the actual context (although far from
"good code" I assume).

The result of this is that an assert fails with the following information:

File: c:\program files\microsoft visual studio 8\vc\include\vector
Line: 117
Expression: ("this->_Mycont != NULL", 0)

This is one of the ++-operators in vector and the assert occurs on the
"i++"-instruction in the code above. _SCL_SECURE_VALIDATE (this->_Mycont !=
NULL) seems to be the assert that fails.

I have read about "Debug iterator support" and "Checked iterators" here:
http://msdn2.microsoft.com/en-us/library/ms235424(VS.80).aspx. It seems to me
that the assert has to do with one of these. I also read that these can be
disabled by using the following:

#define _HAS_ITERATOR_DEBUGGING 0
#define _SECURE_SCL 0

I have put these the first thing in the cpp-file with my main-function. I
still seem to get the assert failure.

Are you using stdafx.h? Remember, any code before #include "stdafx.h" is
completely ignored.

My questions are:
1) What, exactly, is causing the assert failure?

The code is illegal - erasing an element in a vector invalidates
iterators to that element and any element that lies after that element.
Your code increments an invalidated iterator.
2) Is it possible to revert to the VC++ 2003 behavior where there is no
failure?

Surely better to fix the code so that it is correct rather than relying
on "lucky" undefined behaviour?
3) Is my theory about "Debug iterator support" and "Checked iterators" a
dead end or am I just using the #DEFINE:s the wrong way?

The latter I think - the asserts shouldn't be present if you've
correctly switched off the iterator debugging. I think you should
probably add the defines to your project settings, since they must be
the same for all .cpp files to avoid one definition rule violations.
I want to point out that I realize that the obvious solution is to rewrite
the offending parts of the code. However, I have not written the code in
question myself so at this point I do not want to do this. I just want to
recreate the behavior from VC++ 2003 to recreate a functioning program and
then take it from there.

The correct code to remove duplicates from an unsorted vector is
something like this:

std::vector<int> int_vector;

if (int_vector.size () > 1)
{
std::vector<int>::iterator i = int_vector.begin (), j =
int_vector.end ();
for (; i + 1 != j; ++i)
{
j = std::remove (i + 1, j, *i);
}
int_vector.erase (j, int_vector.end ());
}

If the vector is sorted, then std::unique is obviously much better.

Tom
 
A

adebaene

Johan said:
Hi,

I'm trying to "port" a project from VC++ 2003 to VC++ 2005 (Express
Edition). This project contains the following code on several places (It is
not exactly this code but a generalization of what it does.):

vector<int> int_vector;
vector<int>::iterator i, j;

for (i = int_vector.begin (); i != int_vector.end (); i++)
{
for (j = int_vector.begin (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.erase (j);
}
}
}

I realize the reason for doing this is not obvious in the generalization
above but a bit more understandable in the actual context (although far from
"good code" I assume).

What is this code supposed to do?
There will always be a point where i and j are pointing to the same
element in int_vector (when i==int_vector.begin()==j at the first
iteration, for example), and at this point it's obvious that
(*j)==(*i), and then you delete the element. From this point up, the
behaviour is undefined... but what did you expect from the code
exactly? The most "obvious" answer (at least to me), it that you wanted
to empty the vector, but it's certainly not that...
The result of this is that an assert fails with the following information:

File: c:\program files\microsoft visual studio 8\vc\include\vector
Line: 117
Expression: ("this->_Mycont != NULL", 0)

This is one of the ++-operators in vector and the assert occurs on the
"i++"-instruction in the code above. _SCL_SECURE_VALIDATE (this->_Mycont !=
NULL) seems to be the assert that fails.
1) What, exactly, is causing the assert failure?

Your code is buggy : once you have inserted or deleted an item in a
vector, all current iterators on this vector are invalidated
2) Is it possible to revert to the VC++ 2003 behavior where there is no
failure?

Bad idea : the 2005 STL is trying very hard to say you there is a bug
in your code : you'd better listent to it instead of trying to shut
it's mouth with a #define.

Arnaud
MVP - VC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top