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

std::vector reallocation

Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated. My question is: to what will it
point? Does it get reallocated to?
Jul 22 '05 #1
6 1681
Lieven wrote:
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated.
It's actually not just possible, it's very likely.
My question is: to what will it
point? Does it get reallocated to?


No. If the vector is reallocated 'it' will become invalid.

V
Jul 22 '05 #2

"Lieven" <li*************@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated.
Yes, possible, and probable. Also note that your
code above results in a vector containing
10000100 elements, the first hundred of which
have a value of zero.
My question is: to what will it
point?
A vector does not 'point'. A vector is not a pointer.
It's a container which stores objects.
Does it get reallocated to?


When a vector's size increases, if necessary, additional
memory will be automatically allocated to accomodate the
new elements. You can 'preallocate' space for your elements
by using 'vector.resize()' or 'vector.reserve()' if desired.

-Mike
Jul 22 '05 #3
Lieven wrote:
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated. My question is: to what will it
point? Does it get reallocated to?


In almost all implications (but of course not guaranted by the standard)
it will continue pointing to whatever bit of memory the original vector
was allocated to, but now that memory won't be owned by the vector any more.

Offically, reading or writing to it is undefined. Unoffically, you are
likely to find that you think you can get away with reading from it for
some time after reallocation, as most systems won't bother wiping the
memory the vector was in until something else is put there. However of
course DO NOT DO IT! Because it's likely to break in all kinds of
interesting ways and be a very difficult bug to track down.

Chris
Jul 22 '05 #4
Mike Wahler wrote:
"Lieven" <li*************@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated.

Yes, possible, and probable. Also note that your
code above results in a vector containing
10000100 elements, the first hundred of which
have a value of zero.

My question is: to what will it
point?

A vector does not 'point'. A vector is not a pointer.
It's a container which stores objects.


'it' here is a variable, the iterator, initialised with 'vec.begin()'.
[...]


V
Jul 22 '05 #5
Mike Wahler wrote:
My question is: to what will it
point?


A vector does not 'point'. A vector is not a pointer.
It's a container which stores objects.

This is of course my fault for using bad names and typos, but the 'it'
refers to the iterator named 'it'.
Jul 22 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:BD*****************@newsread1.dllstx09.us.to. verio.net...
Mike Wahler wrote:
"Lieven" <li*************@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated.

Yes, possible, and probable. Also note that your
code above results in a vector containing
10000100 elements, the first hundred of which
have a value of zero.

My question is: to what will it
point?

A vector does not 'point'. A vector is not a pointer.
It's a container which stores objects.


'it' here is a variable, the iterator, initialised with 'vec.begin()'.


Ah, right, I see that now. I thought he was using the English word
'it' (referring to the vector).

-Mike
Jul 22 '05 #7

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 counted implementation of std::vector? Thanks.
1
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
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: ...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
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
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.