473,387 Members | 1,516 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.

pointer to an elment in a stl vector

Say I have vector<int> tmp. I then call a function that needs a
pointer to an int in tmp. Is it wise to pass the function &(tmp[i])?
If the vector increases in size could it invalidate that pointer?
Jul 22 '05 #1
4 1363

"Eric" <em*****@myrealbox.com> wrote in message
news:a6*************************@posting.google.co m...
Say I have vector<int> tmp. I then call a function that needs a
pointer to an int in tmp. Is it wise to pass the function &(tmp[i])?
If the vector increases in size could it invalidate that pointer?


not if you stick to adding elements, if you plan on erasing some, you
shouldn't use it, switching to list might help.
Jul 22 '05 #2
Eric wrote:
Say I have vector<int> tmp. I then call a function that needs a
pointer to an int in tmp. Is it wise to pass the function &(tmp[i])?
Yes. AFAIK, it is blessed by the standard.

If the vector increases in size could it invalidate that pointer?


Yes. The vector might be reallocated to accommodate for new elements.
This will invalidate all iterators and pointers derived from iterators.
Best

Kai-Uwe Bux
Jul 22 '05 #3

"Flzw" <fl****@wanadoo.fr> wrote in message
news:cg**********@news-reader2.wanadoo.fr...

"Eric" <em*****@myrealbox.com> wrote in message
news:a6*************************@posting.google.co m...
Say I have vector<int> tmp. I then call a function that needs a
pointer to an int in tmp. Is it wise to pass the function &(tmp[i])?
If the vector increases in size could it invalidate that pointer?
not if you stick to adding elements, if you plan on erasing some, you


Wrong! std::vector::push_back CAN cause invalidation, and most certainly
will if the capacity of the vector is insufficient.

shouldn't use it, switching to list might help.

Jeff F
Jul 22 '05 #4
Kai-Uwe Bux wrote:
Eric wrote:
Say I have vector<int> tmp. I then call a function that needs a
pointer to an int in tmp. Is it wise to pass the function &(tmp[i])?


Yes. AFAIK, it is blessed by the standard.

If the vector increases in size could it invalidate that pointer?


Yes. The vector might be reallocated to accommodate for new elements.
This will invalidate all iterators and pointers derived from
iterators.


And of course also pointers not derived from iterators. It might
invalidate all pointers to any elements of the vector.

Jul 22 '05 #5

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

Similar topics

7
by: john townsley | last post by:
when using vectors with pointers of objects. is it best to create instances then point to them or dynamic allocation upon creating a vector element. I can do this, but is creating a vector with...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
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; ....
7
by: nabeel.girgis | last post by:
I am passing a vector by reference into a function and I am trying to use a pointer in that function. I get an error saying : '=' : cannot convert from 'std::vector<_Ty> *' to 'int *' when I...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
19
by: George | last post by:
Hi all. How can I check and return an error on a constructor that receives a pointer as a parameter and the parameter is null. Is there any possibility to return an error? or do I have to create...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
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
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: 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
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,...
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.