473,406 Members | 2,894 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,406 software developers and data experts.

c++: modifying a vector within a function

Hi,
a question probably posed many times butI couldn't find on the web...
1) i gather that for a vector, v, if v.size() exceeds v.capacity() during a call to v.push_back(x) the allocation of increased space can result in a change in the position of that vector in memory
2) thus if that push_back(x) call occurs in a function where a reference to that vector has been passed, and a reallocation occurs, is that reference broken?

void pushme(vector<int>& w) {
...
w.push_back(32);
...
some other manipulation of w
}

int main() {
vector<int> v;
pushme(v);
}

Thanks!
May 2 '06 #1
2 5236
Banfa
9,065 Expert Mod 8TB
No the memory allocated to the vector to store it's contents is not the same as the vector object itself and since the reference is to the vector object and not it's contained memory it remains valid. If you took a reference or pointer to a member of the vector then that could become invalid after an operation that could result in a memory reallocation like so

Expand|Select|Wrap|Line Numbers
  1. void pushme(vector<int>& w) {
  2.     ...
  3.     w.push_back(32);
  4.     w.push_back(64);
  5.     w.push_back(128);
  6.     ...
  7.     int &w2 = w[2];
  8.     ...
  9.     w.push_back(256);
  10.     w.push_back(512);
  11.     w.push_back(1024);
  12.     ...
  13.     // At this point w is still valid but w2 may be invalid
  14. }
  15.  
May 3 '06 #2
Thanks for a very clear response! :)
May 3 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
16
by: Honestmath | last post by:
Hi, I added the following line to my code within a class declaration: std::vector<Date> m_duedates(100); I also tried: std::vector<Date> m_duedates(100, Date());
11
by: Jason Heyes | last post by:
Look at my code: void modify_element(BigType &x) { /* not shown */ } BigType modify_element_copy(BigType x) { modify_element(x);
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
2
by: mj | last post by:
Hi, I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
3
by: clinisbut | last post by:
I'm trying to understand what's wrong with this: void getAllData( std::vector<unsigned char&char_array ) { char_array.clear(); char_array.reserve( 11 ); char_array = 'A'; //<----No...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.