473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Assertion failed in vector iterator

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/lib...24(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.

Mar 30 '06 #1
4 6355
> 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);
}
}
}


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.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 30 '06 #2
Johan Pettersson wrote:
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/lib...24(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
Mar 30 '06 #3

Johan Pettersson wrote:
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.
<snip> 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

Mar 30 '06 #4
Thanks for the help (both of you)! I will rewrite the code like you suggest.

"Tom Widmer [VC++ MVP]" wrote:
Johan Pettersson wrote:
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/lib...24(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

Mar 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kostatus | last post by:
When I close my program and call: delete *iter2; (iter2 being an iterator of a vector which contains pointers to objects) I get a "Debug Assertion Failed!" message (using VC++ 6) with the...
14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
2
by: marat | last post by:
I have a managed c++ function below that makes a call to an unmanaged c++ dll .. This function is called from a C# app, resulting in a "Debug Assertion Failed dbgdel.cpp Line 52 Expression:...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
1
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made...
1
by: themadme | last post by:
im running code thats using threads, everytime i get this debug assertion failed box poping up. It involves with my stl vector. its saying that vector iterators incompatible. the vector...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.