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

Vector pointers

Hi,

I am trying to find if its at all possible to create a pointer to an
object inside a vector, based upon a vector iterator that will remain
a valid pointer once the iterator is invalid.

For example I just tried:

for(vector<ship>::iterator invader = shooters.begin(); invader !=
shooters.end(); )
{
....
Ship.parent = &(*invader);
....
}

With the aim being that parent points to the item inside the invader
vector throughout the life of the vector, but instead the pointer
becomes invalid when the iterator is invalidated.

Any solutions to this problem?

Thanks
Jack

Nov 9 '07 #1
3 1973
JackC wrote:
Hi,

I am trying to find if its at all possible to create a pointer to an
object inside a vector, based upon a vector iterator that will remain
a valid pointer once the iterator is invalid.

For example I just tried:

for(vector<ship>::iterator invader = shooters.begin(); invader !=
shooters.end(); )
{
...
Ship.parent = &(*invader);
...
}

With the aim being that parent points to the item inside the invader
vector throughout the life of the vector, but instead the pointer
becomes invalid when the iterator is invalidated.

Any solutions to this problem?
If the vector reallocates, invalidating the iterator, your address is
also invalidated. Why not store the index?

for (vector<ship>::size_type i = 0 ; i < shooters.size(); ++i)
{
...
Ship.parent_index = i;
...
}
Nov 9 '07 #2
On 9 Nov, 18:22, red floyd <no.s...@here.dudewrote:
JackC wrote:
Hi,
I am trying to find if its at all possible to create a pointer to an
object inside a vector, based upon a vector iterator that will remain
a valid pointer once the iterator is invalid.
For example I just tried:
for(vector<ship>::iterator invader = shooters.begin(); invader !=
shooters.end(); )
{
...
Ship.parent = &(*invader);
...
}
With the aim being that parent points to the item inside the invader
vector throughout the life of the vector, but instead the pointer
becomes invalid when the iterator is invalidated.
Any solutions to this problem?

If the vector reallocates, invalidating the iterator, your address is
also invalidated. Why not store the index?

for (vector<ship>::size_type i = 0 ; i < shooters.size(); ++i)
{
...
Ship.parent_index = i;
...

}
Thanks alot, don't know why i didn't think of doing that.

Nov 9 '07 #3
"JackC" <je******@gmail.comwrote in message
news:11**********************@o38g2000hse.googlegr oups.com...
Hi,

I am trying to find if its at all possible to create a pointer to an
object inside a vector, based upon a vector iterator that will remain
a valid pointer once the iterator is invalid.

For example I just tried:

for(vector<ship>::iterator invader = shooters.begin(); invader !=
shooters.end(); )
{
...
Ship.parent = &(*invader);
...
}

With the aim being that parent points to the item inside the invader
vector throughout the life of the vector, but instead the pointer
becomes invalid when the iterator is invalidated.

Any solutions to this problem?
Not really. When the vector invalidates an iterator, that generally means
that the object itself has moved in memory. If it has moved in memory, then
your pointer is going to become invalid. Now, you can save the index of it,
but that index can also change (something was deleted in front of this one,
shifting it's index down one).

The only real way to fix this if you have to do it is to either store
pointers in the first place (using new, delete and pushing the pointer) or
use some other type of container where the index won't be invalidated, or
that uses a key you can store.

It all depends on how important it is to you to have a pointer to the
instance that doesn't become invalidated.

Nov 9 '07 #4

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

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
11
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;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.