vector内存连续性的疑问
假设:vector<int> v 中已经有4个元素了。 这4个元素耗去了 16字节内存。此时 内存池中只有2个8字节的块。如果 有这样的一句代码:v.reseve(8); 意味着,v还需要16字节。由于16字节小于128,所以进入第二级配置器。问题:内存池会将这2个8字节的块 直接拨给vector吗?不会给vector,因为这2个8字节的块,直接拨给v, 但是 很可能 v以前的内存和 这2个8自己的内存 没有连续,那么是无法使用的。 那么stl如何判断 vector需要连续性内存呢??
void reserve(size_type _Count) { // determine new minimum length of allocated storage if (max_size() < _Count) _Xlen(); // result too long else if (capacity() < _Count) { // not enough room, reallocate pointer _Ptr = this->_Alval.allocate(_Count); _TRY_BEGIN _Umove(this->_Myfirst, this->_Mylast, _Ptr); _CATCH_ALL this->_Alval.deallocate(_Ptr, _Count); _RERAISE; _CATCH_END size_type _Size = size(); if (this->_Myfirst != 0) { // destroy and deallocate old array _Destroy(this->_Myfirst, this->_Mylast); this->_Alval.deallocate(this->_Myfirst, this->_Myend - this->_Myfirst); } this->_Orphan_all(); this->_Myend = _Ptr + _Count; this->_Mylast = _Ptr + _Size; this->_Myfirst = _Ptr; } }