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_ba ck() 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_ba ck(index_count) ;
indices.push_ba ck(new int[index_count]);
if (!indices[i])
return false;
in.readInt32(in dices[i], index_count);
}
--nico 3 9359
"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_ba ck() 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_D EBUGGING 0 to prevent the invalidation,
but I don't recommend it unless you're really sure about what you're doing.
Sean
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_D EBUGGING 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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), an iterator that I've set in a
loop gets changed. Here's an example of the problem (sorry about the
lack of indentation, posting this from Google):
...
|
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 circumstances in
which the STL will move a vector, or invalidate iterators to elements
in the vector, if you don't insert or remove elements?
My actual...
|
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;
|
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
}
|
by: zaineb |
last post by:
Hi,
This is a follow-up of sort of this thread:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/de12af6539e0d645/662cab752779f1fa?lnk=st&q=c%2B%2B+vector+vs+map&rnum=1&hl=en#662cab752779f1fa
I've been chewing on this problem for a while. When I came across the
above thread, I figured that maybe other members have a better...
| |
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() == 0.
I'm hoping that the above expression is fine, even if v is empty, but
|
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 instead I don't need to supply the iterator. But what
|
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' and 'v_end' are valid iterators pointing to some
|
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 this?
I wrote the following program to test this out.
#include <fstream>
#include <iostream>
#include <string>
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |