std::vector reallocation

L

Lieven

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?
 
V

Victor Bazarov

Lieven said:
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
 
M

Mike Wahler

Lieven said:
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
 
C

chris

Lieven said:
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
 
V

Victor Bazarov

Mike said:
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.




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
 
L

Lieven

Mike said:
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'.
 
M

Mike Wahler

Victor Bazarov said:
'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top