472,328 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Strange vector iterator problem - possible bug

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):

#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> T;
typedef T::iterator I;

T foo;

foo.push_back(1);

for (I i = foo.begin(); i != foo.end(); ++i) {

// Output of this is 0.
cout << (i - foo.begin());

foo.push_back(0);

// Output of this is -8.
cout << (i - foo.begin());

}

Now, I know that foo.end() will change because of the push_back and
this would create an infinite loop, but that's not an issue in my code,
I've just left out the irrelevant parts of the loop. I've put those
cout()s directly around the push_back(), and that's what I got. Should
an iterator be completely invalidated by a push_back? If so, is there a
way to re-align the iterator without something like "tmp = i -
foo.begin(); foo.push_back(0); i = foo.begin() + tmp;"? If not, and
this is a bug, how do I go about reporting this problem to the GCC
devs?

Thanks for your time and help!

Jul 22 '05 #1
2 2302
Dave wrote:
When calling vector<int>::push_back(0), an iterator that I've set in a loop gets changed.
No, it isn't changed, it is invalidated! The 'vector's internal array
is moved to a different location when inserting more items if
insufficient memory was allocated. You can avoid this by using
'reserve()'.
Here's an example of the problem (sorry about the
lack of indentation, posting this from Google):
I'm also posting through the new Google interface which *sucks*. I'm
prefixing lines with a pipe to allow readable indentation...
#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> T;
typedef T::iterator I;

T foo;

foo.push_back(1);

for (I i = foo.begin(); i != foo.end(); ++i) {

// Output of this is 0.
cout << (i - foo.begin());

foo.push_back(0);
This line invalidates all iterators, pointers, and reference to 'foo'
or its elements. This is expected behavior (although the standard makes
no guarantee when exactly the iterators are really invalidated: an
implementation may choose to overallocate the elements).
// Output of this is -8.
cout << (i - foo.begin());
The "-8" is just an accidental reasonably sized value. It could be
anything. You can avoid this problem by inserting

foo.reserve(16);

prior to the loop (well, the problem will occur at some point anyway
because you created an infinite loop as far as I can tell).
}

Now, I know that foo.end() will change because of the push_back and
this would create an infinite loop, but that's not an issue in my code, I've just left out the irrelevant parts of the loop. I've put those
cout()s directly around the push_back(), and that's what I got. Should an iterator be completely invalidated by a push_back?
Invalidating iterators normally has no impact on the iterator itself:
it is just unchanged but using it is a programming error. There are
some STL implementations (e.g. Safe STL) which try to capture problems
like this and give meaningful error messages.
If so, is there a
way to re-align the iterator without something like "tmp = i -
foo.begin(); foo.push_back(0); i = foo.begin() + tmp;"?
You can a priori 'reserve()' an approriate number of elements. If this
is unfeasable, you need to realign your iterators in a way similar to
what you have posted. Of course, you may also choose to use indices in
this case as these don't get invalidated unless you shrink the
'vector'.
If not, and
this is a bug, how do I go about reporting this problem to the GCC
devs?


It isn't a bug.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2
Thank you very much! It's so nice to get a clear and complete answer :)

Jul 22 '05 #3

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

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing,...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector...
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...
14
by: cayblood | last post by:
I want to iterate through a vector and erase elements that meet a certain criteria. I know there is an algorithmic way of doing this, but first I...
33
by: Simon | last post by:
Hi, I am going to use quite a few vectors and I want to make sure I am using it properly. // struct MYSTRUCT { .... }
4
by: Johan Pettersson | last post by:
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...
4
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.