473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector reallocation

Consider:

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

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

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

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 1699
Lieven wrote:
Consider:

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

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

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

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>::it erator it(vec.begin()) ;

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

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>::it erator it(vec.begin()) ;

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

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<i nt> vec(100);

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

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

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.********@com Acast.net> wrote in message
news:BD******** *********@newsr ead1.dllstx09.u s.to.verio.net. ..
Mike Wahler wrote:
"Lieven" <li************ *@hotmail.com> wrote in message
news:41******** *************** @news.skynet.be ...
Consider:

std::vector<i nt> vec(100);

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

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

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
5959
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
2307
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 the vector, and add another element which will always be at the end, ie. vector<int> v; int i, x; .... initialization
18
2868
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 contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
20
17828
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
8
5109
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69682
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: std::vector<int> fun(){ //build the vector v; return v; }
7
3005
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 501st element is going to cause some more allocation -- for arguments sake if the vector re-grows to accomodate 1000 elements, I play around with it and completely erase everything in it after I am done. Now how much does the vector hold? Do I...
56
5791
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
2956
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
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9064
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.