change of vectors first pointer
c++, insert, pointers, vector
Solution
std::vector can dynamically resize. The way it does this is to hold more space than is required. Once you hit the reserved capacity a larger block of data needs to be reserved (depends on implementation, but often the capacity of the new block is double the previous size). The data is then copied across to the new location, which is why the address of the first element changes.
Provided you don't add more data to your vector you don't have to worry about a pointer to data in the vector being invalidated. You can anticipate when a further push_back() will trigger a resize by checking if vint.capacity()==vint.size(). You can also avoid using an invalidated pointer by always using the up-to-date one via &vint[0] (or &vint.at() to get the range checking) rather than copying it.
If you know that you are going to insert a number of new items, you can ensure you will have enough capacity by using reserve() to preallocate the space (if the current capacity is less than what's requested). But be aware that you don't want to do this piecemeal - for instance
vint.reserve(vint.size() + 2000);
for(int i=0; i<2000; ++i) {
vint.push_back(i);
}
would be fine, but
for(int i=0; i<2000; ++i) {
vint.reserve(vint.size() + 1);
vint.push_back(i);
}
would be a performance drag since you're repeatedly asking the OS for more memory and incurring an ever-larger copy operation with each iteration.
Problem
When i use vector to store some data, I usually access to this data by the pointer of the vector's first entry. because it it faster than the at() method. But I realize that when I insert a block of data, say an array to the end of vector, the first entry's pointer changes. This may be realated to stack stuff, But if I add the array one element at a time by push_back, first pointer does not change. So why is that? Should i worry about using a pointer to access the elements? Here is a sample code for those who wants to check out: ``` int arrayLen = 500000; vector<int> vint = vector<int>(2000,0); int * firstEntry = &vint[0]; int * iarray = new int[arrayLen]; for(int i = 0; i< arrayLen; i++) { iarray[i] = i; } vint.insert(vint.end(),iarray,iarray+arrayLen); cout << firstEntry << "," << &vint[0] << endl; // They ar not equal; // reset the vector vint.clear(); vint.resize(2000,0); firstEntry = &vint[0]; for(int i = 0; i< arrayLen; i++) { vint.push_back(iarray[i]); if(firstEntry != &vint[0]) cout << firstEntry << "," << &vint[0] <<","<< i << endl; }// nothing is written cout << firstEntry << "," << &vint[0] << endl; // now they are equal; ```