473,395 Members | 1,694 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,395 software developers and data experts.

iterator problem

All these happen in VC++ NET 2003, if you ask me about the compiler.
it._Myptr is a VC++ specific member of the iterator implementation.
But from this, we extract usefull informations about crash.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0 (what pointer
is this?)
v.insert(it, 5); // works
it++; // it._Myptr == 4
v.insert(it, 5); // crashes
----------------------------
and this.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0
v.push_back(5);
vector<int*>::iterator it = v.begin(); // it._Myptr == 0x00323b40 (the
pointer to first element)
----------------------------
All of these are compiler bugs on iterator implementation, or I miss
something?

Thanks
Feb 4 '06 #1
4 3421
Chameleon wrote:
All these happen in VC++ NET 2003, if you ask me about the compiler.
it._Myptr is a VC++ specific member of the iterator implementation.
But from this, we extract usefull informations about crash.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0 (what
pointer is this?)
The same as v.end(). No bug. You're not going to dereference it.
v.insert(it, 5); // works
That's okay, but invalidates "it".
it++; // it._Myptr == 4
Unfortunately, "it" is invalid.
v.insert(it, 5); // crashes
Yup.
----------------------------
and this.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0
v.push_back(5);
This compiles?
vector<int*>::iterator it = v.begin(); // it._Myptr == 0x00323b40
(the pointer to first element)
----------------------------
All of these are compiler bugs on iterator implementation, or I miss
something?


Ah, not every error is a compiler error!

Cheers, Calum

Feb 4 '06 #2
In article <ds**********@volcano1.grnet.gr>,
Chameleon <ch******@hotmail.NOSPAM.com> wrote:
All of these are compiler bugs on iterator implementation, or I miss
something?


Until you know enough that you can build your own compiler, go ahead and
assume that the bug is in your code rather than the compiler's code...
Just a thought.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 5 '06 #3
Calum Grant wrote:
Chameleon wrote:
All these happen in VC++ NET 2003, if you ask me about the compiler.
it._Myptr is a VC++ specific member of the iterator implementation.
But from this, we extract usefull informations about crash.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0 (what
pointer is this?)


The same as v.end(). No bug. You're not going to dereference it.
v.insert(it, 5); // works


That's okay, but invalidates "it".
it++; // it._Myptr == 4


Unfortunately, "it" is invalid.
v.insert(it, 5); // crashes


Yup.
----------------------------
and this.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0
v.push_back(5);


This compiles?


sorry, vector<int> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0x00323b40
(the pointer to first element)
----------------------------
All of these are compiler bugs on iterator implementation, or I miss
something?


Ah, not every error is a compiler error!

of-course!

my solution is this until now:

-------------------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin();
it = v.insert(it, 5);
it++;
it = v.insert(it, 5);
-------------------------------------
Feb 5 '06 #4
Chameleon wrote:
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0
v.push_back(5);
This compiles?


sorry, vector<int> v;


Okay, vector<int>. Good.
my solution is this until now:

-------------------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin();
it = v.insert(it, 5);
it++;
it = v.insert(it, 5);
-------------------------------------


Back to vector<int*> now? Well, in any case... line 2 above serves no
purpose. I would change this snippet to:

std::vector<int> v;
v.push_back(5);
v.push_back(5);

See? No need for iterators at all in this case. Maybe you got
confused because you didn't know about push_back? You owe it to
yourself to learn the public interface of important classes like
std::vector. It is only wafer-thin.

Luke

Feb 5 '06 #5

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
5
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.