473,804 Members | 3,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::it erator i, j;

for (i = int_vector.begi n (); i != int_vector.end (); i++)
{
for (j = int_vector.begi n (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.eras e (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\ve ctor
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_VAL IDATE (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_D EBUGGING 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 6379
> vector<int> int_vector;
vector<int>::it erator i, j;

for (i = int_vector.begi n (); i != int_vector.end (); i++)
{
for (j = int_vector.begi n (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.eras e (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_D EBUGGING 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************* *********@hotma il.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>::it erator i, j;

for (i = int_vector.begi n (); i != int_vector.end (); i++)
{
for (j = int_vector.begi n (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.eras e (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\ve ctor
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_VAL IDATE (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_D EBUGGING 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.siz e () > 1)
{
std::vector<int >::iterator i = int_vector.begi n (), j =
int_vector.end ();
for (; i + 1 != j; ++i)
{
j = std::remove (i + 1, j, *i);
}
int_vector.eras e (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>::it erator i, j;

for (i = int_vector.begi n (); i != int_vector.end (); i++)
{
for (j = int_vector.begi n (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.eras e (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.b egin()==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\ve ctor
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_VAL IDATE (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>::it erator i, j;

for (i = int_vector.begi n (); i != int_vector.end (); i++)
{
for (j = int_vector.begi n (); j != int_vector.end (); j++)
{
if ((*j) == (*i))
{
int_vector.eras e (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\ve ctor
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_VAL IDATE (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_D EBUGGING 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.siz e () > 1)
{
std::vector<int >::iterator i = int_vector.begi n (), j =
int_vector.end ();
for (; i + 1 != j; ++i)
{
j = std::remove (i + 1, j, *i);
}
int_vector.eras e (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
12234
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 following details: File: dbgheap.c Line: 1017 Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) What does this mean?
14
5029
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 points in space that arrive randomly into groupings based on (x, y, z) dimensions, so I use std::map to do that. Once the sorting is done I need to find the interactions between groups, so I've been using iterators to step through the map. As a...
18
2882
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 contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
3
2928
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 kinds of iterators on say a vector. OR is it that any iterator declared on Vector is Random Iterator which has the functionality of all the others.
2
8408
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: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" errir message. This happens on function exit, looks like related to destruction of some vector. Any ideas why ????
9
5217
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); vector<double>::iterator i=x.begin()+2, j=x.begin()+6; x.erase(i,j); // i<j, ok, erases 4 elements. x.erase(j,i); // j>i, no error, just inserts 4 elements.
4
5709
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 vector
1
6231
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 of vectors of vectors template< typename T > matrix< T >::matrix( unsigned rows, unsigned columns ) { vector< vector< T ::iterator p;
1
4171
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 contains structs of data. i have ten threads running, using the same function called "GetMemory()". the function just looks through the function and swaps around the data in the vector list. The first ten containers in the vector are the Main...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.