C++ std::vector memory/allocation
- by aaa
from a previous question about vector capacity, http://stackoverflow.com/questions/2663170/stdvector-capacity-after-copying, Mr. Bailey said:
In current C++ you are guaranteed that no reallocation occurs after a call to reserve until an insertion would take the size beyond the value of the previous call to reserve. Before a call to reserve, or after a call to reserve when the size is between the value of the previous call to reserve and the capacity the implementation is allowed to reallocate early if it so chooses.
So, if I understand correctly, in order to assure that no relocation happens until capacity is exceeded, I must do reserve twice? can you please clarify it?
I am using vector as a memory stack like this:
std::vector<double> memory;
memory.reserve(size);
memory.insert(memory.end(), matrix.data().begin(), matrix.data().end()); // smaller than size
size_t offset = memory.size();
memory.resize(memory.capacity(), 0);
I need to guarantee that relocation does not happen in the above.
thank you.
ps: I would also like to know if there is a better way to manage memory stack in similar manner other than vector