Rozważmy następujące kodyemplace_back() problem pod VS2013
std::vector<int> nums{21, 22, 23, 24};
nums.emplace_back(nums[0]);
nums.emplace_back(nums[1]);
for (auto n : nums) {
std::cout << n << std::endl;
}
wyjściu VS2013
21
22
23
24
-17891602
22
Dlaczego -17891602
jest tutaj?
Wyjście GCC 4.8.4
jest poprawna następująco
21
22
23
24
21
22
Potem porównać realizację emplace_back
między VS2013
i GCC
VS2013
template<class... _Valty>
void emplace_back(_Valty&&... _Val)
{ // insert by moving into element at end
if (this->_Mylast == this->_Myend)
_Reserve(1);
_Orphan_range(this->_Mylast, this->_Mylast);
this->_Getal().construct(this->_Mylast,
_STD forward<_Valty>(_Val)...);
++this->_Mylast;
}
GCC
template<typename _Tp, typename _Alloc>
template<typename... _Args>
void
vector<_Tp, _Alloc>::
emplace_back(_Args&&... __args)
{
if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
{
_Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
std::forward<_Args>(__args)...);
++this->_M_impl._M_finish;
}
else
_M_emplace_back_aux(std::forward<_Args>(__args)...);
}
Wydaje się, że dziwne _Reserve(1);
jest używane w VS2013
. Czemu?
Edit:
Wartość hex
z -17891602
jest 0xFEEEFEEE
, co oznacza
używanego przez debugowania HeapFree Microsoftu(), aby zaznaczyć pamięć zwolniona sterty
Patrz magic number
Następnie debugowałem powyższą linię kodów linkiem e i znalazłem wywołanie 0XFEEEFEEE
spowodowane przez _Reserve(1);
.
Możliwy duplikat: [Czy std :: wektor emplace_back kopiuje konstrukcję z elementu samego wektora?] (Http://stackoverflow.com/questions/24908718/can-stdvector-emplace-back-copy-construct-from -element-of-the-vector-sam w sobie). – cpplearner
W tym pytaniu chcę wiedzieć, dlaczego ''Reserve (1)' jest używane w 'VS2013'? różni się linkiem powyżej. – zangw