473,503 Members | 7,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector iterators invalidated and DEBUG_ITERATORS

Hi,

compiling a program with the code blurb below causes a runtime error
("Expression: vector iterators incompatible") due to the debug
iterators in VC++ 8. The error happens in the ind_len.push_back() call,
at the second iteration. I suspect that the iterator has been
invalidated, but I can't see how this would be from one iteration to
another in this tiny for loop. Could anyone give me more insight?
Thanks!

std::vector<int*> indices;
std::vector<int> ind_len;

[...]

int patch_count = in.readUInt32();
for (int i = 0; i < patch_count; i++)
{
int index_count = in.readUInt32();
ind_len.push_back(index_count);
indices.push_back(new int[index_count]);
if (!indices[i])
return false;
in.readInt32(indices[i], index_count);
}

--nico

May 19 '06 #1
3 9351
"ng******@gmail.com" wrote:
compiling a program with the code blurb below causes a runtime error
("Expression: vector iterators incompatible") due to the debug
iterators in VC++ 8. The error happens in the ind_len.push_back() call,
at the second iteration. I suspect that the iterator has been
invalidated, but I can't see how this would be from one iteration to
another in this tiny for loop. Could anyone give me more insight?


In VC++ 8, iterators into vectors become invalidated whenever the number of
elements in the vector changes. The invalidation happens immediately. The
size of the loop is irrelevant.

You can use #define _HAS_ITERATOR_DEBUGGING 0 to prevent the invalidation,
but I don't recommend it unless you're really sure about what you're doing.

Sean
May 19 '06 #2
Sean M. DonCarlos wrote:
In VC++ 8, iterators into vectors become invalidated whenever the number of
elements in the vector changes. The invalidation happens immediately. The
size of the loop is irrelevant.

You can use #define _HAS_ITERATOR_DEBUGGING 0 to prevent the invalidation,
but I don't recommend it unless you're really sure about what you're doing.


Thanks for your reply. I did know about the work-around, but I'm not
confident enough to simply apply it, especially because I don't know
what is going on here. Anyway, a simple loop pushing data onto the end
of a vector shouldn't be a problem, should it? I understand that
iterators into the vector are being invalidated by the push_back(), but
I'm not holding, nor using iterators into the vector.

--nico

May 19 '06 #3
On 19 May 2006 13:46:05 -0700, "ng******@gmail.com" <ng******@gmail.com>
wrote:
Thanks for your reply. I did know about the work-around, but I'm not
confident enough to simply apply it, especially because I don't know
what is going on here. Anyway, a simple loop pushing data onto the end
of a vector shouldn't be a problem, should it?
It will invalidate end(), but unless reallocation happens, it won't
invalidate any other iterator. You can prevent reallocation by using
reserve() beforehand.
I understand that
iterators into the vector are being invalidated by the push_back(), but
I'm not holding, nor using iterators into the vector.


Can you post a small console program that demonstrates the problem?

--
Doug Harrison
Visual C++ MVP
May 19 '06 #4

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

Similar topics

2
2391
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
11
2882
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
11
2719
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
8
5008
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
13
20108
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread:...
18
2932
by: John Salmon | last post by:
I was under the impression that the following was always valid: std::vector<Tv; .. T *p = &(v); But I was recently told that care was needed in case the vector was empty, i.e., when v.size()...
12
4893
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
1
2525
by: subramanian100in | last post by:
Suppose I have vector<intvi; deque<intdi; list<intli; Suppose all of these containers have some elements. Suppose 'v_iter' is an iterator pointing to some element in 'vi'. Suppose 'v_beg'...
13
1897
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
0
7194
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
7070
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...
0
7316
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...
1
6976
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...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
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,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.