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

q: stl vector of pointers / pointer consistancy

Hi. I have a question. If I have a vector of objects and on of object
pointers:

---
vector <Obj> objects;
vector <Obj*> objectPointers;
---

and i setup the vectors with somhthing like:

---
Obj o;
objects.push_back(o);
objectPointers.push_back(&objects[objects.size()-1])
---

now. when i grow the objects array:

---
for(int i=0;i<10000;i++)
{
Obj o;
objects.push_back(o);
}
---

is it possible that the pointer in the objectPointer array become
invalid when STL shifts the objects vector around in memory?

thanks!

Oliver

Jul 23 '05 #1
2 1307

"laniik" <la****@yahoo.com> skrev i en meddelelse
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi. I have a question. If I have a vector of objects and on of object
pointers:

---
vector <Obj> objects;
vector <Obj*> objectPointers;
---

and i setup the vectors with somhthing like:

---
Obj o;
objects.push_back(o);
objectPointers.push_back(&objects[objects.size()-1])
---

now. when i grow the objects array:

---
for(int i=0;i<10000;i++)
{
Obj o;
objects.push_back(o);
}
---

is it possible that the pointer in the objectPointer array become
invalid when STL shifts the objects vector around in memory? It is not only possible but almost guaranteed the way your code is written.

/Peter

thanks!

Oliver

Jul 23 '05 #2
laniik wrote:
Hi. I have a question. If I have a vector of objects and on of object
pointers:

---
vector <Obj> objects;
vector <Obj*> objectPointers;
---

and i setup the vectors with somhthing like:

---
Obj o;
objects.push_back(o);
objectPointers.push_back(&objects[objects.size()-1])
objectPointers.push_back(&objects.back())
now. when i grow the objects array:

---
for(int i=0;i<10000;i++)
{
Obj o;
objects.push_back(o);
}
---

is it possible that the pointer in the objectPointer array become
invalid when STL shifts the objects vector around in memory?


Yes. If objects reallocates its memory, all the pointers of objectPointers
will be invalidated.
One way to avoid that is by reserving space. In your example, put
objects.reserve(10000); before the loop. Then, enough memory for 10000
objects gets reserved, so as long as not more objects are added, the vector
will not reallocate.
Of course that is only an option if you know the size in advance.
Another solution is to not store pointers, but indexes. Those won't get
invalidated by push_back().

Jul 23 '05 #3

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

Similar topics

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;
8
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 }
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
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...
9
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
8
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817366 Hi, I have the following question: I make a list whose elements are instances of class B; list<class B>;
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;...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
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:
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
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.