C++11 initializer_list 和 Range-based for loop 学习理解
win10 + vs2017
?
源碼如下:
int main()
{
vector< int > numbers = { 1, 2, 3, 4, 5 };
for (auto num : numbers)
{
printf( "num = %d\n", num );
}
return 0;
}
?
匯編理解如下:
int main()
{
# 入棧
00933F63? sub???????? esp,150h ?
00933F69? push??????? ebx ?
00933F6A? push??????? esi ?
00933F6B? push??????? edi ?
00933F6C? lea???????? edi,[ebp-150h] ?
00933F72? mov???????? ecx,54h ?
00933F77? mov???????? eax,0CCCCCCCCh ?
00933F7C? rep stos??? dword ptr es:[edi] ?
00933F7E? mov???????? eax,dword ptr [__security_cookie (093E004h)] ?
00933F83? xor???????? eax,ebp ?
00933F85? mov???????? dword ptr [ebp-4],eax ?
?? ?vector< int >?? ?numbers = { 1, 2, 3, 4, 5 };
// memset( &numbers, 0x00, sizeof ( numbers ) );
// sizeof ( numbers ) == 10h
00933F88? push??????? 10h ?
00933F8A? lea???????? ecx,[numbers] ?
00933F8D? call??????? std::vector<int,std::allocator<int> >::__autoclassinit2 (09314ABh) ?
// [ebp-14Ch, ebp-138h) 這段內(nèi)存是5個整形(20個字節(jié)),分別賦值為1,2,3,4,5
00933F92? mov???????? dword ptr [ebp-14Ch],1 ?
00933F9C? mov???????? dword ptr [ebp-148h],2 ?
00933FA6? mov???????? dword ptr [ebp-144h],3 ?
00933FB0? mov???????? dword ptr [ebp-140h],4 ?
00933FBA? mov???????? dword ptr [ebp-13Ch],5 ?
// 創(chuàng)建allocator對象,供后面vector構(gòu)造使用,地址為 ebp-111h
00933FC4? lea???????? ecx,[ebp-111h] ?
00933FCA? call??????? std::allocator<int>::allocator<int> (0931163h) ?
00933FCF? push??????? eax ?
// std::initializer_list<int> 構(gòu)造
// initializer_list(const _Elem *_First_arg, const _Elem *_Last_arg)
00933FD0? lea???????? eax,[ebp-138h] ?
00933FD6? push??????? eax ?
00933FD7? lea???????? ecx,[ebp-14Ch] ?
00933FDD? push??????? ecx ?
00933FDE? lea???????? ecx,[ebp-124h] ?
00933FE4? call??????? std::initializer_list<int>::initializer_list<int> (09314A6h) ?
// vector 構(gòu)造
// vector(initializer_list<_Ty> _Ilist, const _Alloc& _Al = _Alloc())
// _Al參數(shù)是在 00933FCF 位置壓棧的
// _Ilist參數(shù)是按值傳遞,將成員_First和_Last分兩次壓棧,對應下面4行
00933FE9? mov???????? edx,dword ptr [eax+4] ?
00933FEC? push??????? edx ?
00933FED? mov???????? eax,dword ptr [eax] ?
00933FEF? push??????? eax ?
00933FF0? lea???????? ecx,[numbers] ?
00933FF3? call??????? std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (0931168h) ?
?? ?for (auto num : numbers)
// std::vector<int,std::allocator<int> >::begin() 結(jié)果保存在 dword ptr [ebp-30h]
00933FF8? lea???????? eax,[numbers] ?
00933FFB? mov???????? dword ptr [ebp-24h],eax ?
00933FFE? mov???????? ecx,dword ptr [ebp-24h] ?
00934001? call??????? std::vector<int,std::allocator<int> >::_Unchecked_begin (09311FEh) ?
00934006? mov???????? dword ptr [ebp-30h],eax ?
// std::vector<int,std::allocator<int> >::end() 結(jié)果保存在 dword ptr [ebp-3Ch]
00934009? mov???????? ecx,dword ptr [ebp-24h] ?
0093400C? call??????? std::vector<int,std::allocator<int> >::_Unchecked_end (09312C1h) ?
00934011? mov???????? dword ptr [ebp-3Ch],eax ?
// 跳轉(zhuǎn) for 循環(huán)的條件比較
00934014? jmp???????? main+0BFh (093401Fh) ?
// 迭代器加1
00934016? mov???????? eax,dword ptr [ebp-30h] ?
00934019? add???????? eax,4 ?
0093401C? mov???????? dword ptr [ebp-30h],eax ?
// eax = dword ptr [ebp-30h]
// eax 和 vector::end() 比較,如果相等則跳出循環(huán)
0093401F? mov???????? eax,dword ptr [ebp-30h] ?
00934022? cmp???????? eax,dword ptr [ebp-3Ch] ?
00934025? je????????? main+0E2h (0934042h) ?
// 將 dword ptr [ebp-30h] 迭代器指向的整形數(shù)值取出來,放到 dword ptr [ebp-48h]
00934027? mov???????? eax,dword ptr [ebp-30h] ?
0093402A? mov???????? ecx,dword ptr [eax] ?
0093402C? mov???????? dword ptr [ebp-48h],ecx ?
?? ?{
?? ??? ?printf( "num = %d\n", num );
// 從 dword ptr [ebp-48h] 取出整形數(shù)值,壓棧
// 將 "num = %d\n" 壓棧
// 調(diào)用 printf
0093402F? mov???????? eax,dword ptr [ebp-48h] ?
00934032? push??????? eax ?
00934033? push??????? offset string "num = %d\n" (093BC88h) ?
00934038? call??????? _printf (093153Ch) ?
0093403D? add???????? esp,8 ?
?? ?}
// 跳轉(zhuǎn)到迭代器加1位置
00934040? jmp???????? main+0B6h (0934016h) ?
??? return 0;
00934042? mov???????? dword ptr [ebp-130h],0 ?
// vecotr析構(gòu)
0093404C? lea???????? ecx,[numbers] ?
0093404F? call??????? std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (0931078h) ?
// 將返回值0放入eax寄存器
00934054? mov???????? eax,dword ptr [ebp-130h] ?
}
// 出棧
0093405A? push??????? edx ?
0093405B? mov???????? ecx,ebp ?
0093405D? push??????? eax ?
0093405E? lea???????? edx,ds:[93408Ch] ?
00934064? call??????? @_RTC_CheckStackVars@8 (09313F7h) ?
00934069? pop???????? eax ?
0093406A? pop???????? edx ?
0093406B? pop???????? edi ?
0093406C? pop???????? esi ?
0093406D? pop???????? ebx ?
0093406E? mov???????? ecx,dword ptr [ebp-4] ?
00934071? xor???????? ecx,ebp ?
00934073? call??????? @__security_check_cookie@4 (0931415h) ?
00934078? add???????? esp,150h ?
0093407E? cmp???????? ebp,esp ?
00934080? call??????? __RTC_CheckEsp (0931212h) ?
00934085? mov???????? esp,ebp ?
00934087? pop???????? ebp ?
00934088? ret ?
轉(zhuǎn)載于:https://www.cnblogs.com/duanjj0929/p/10485884.html
總結(jié)
以上是生活随笔為你收集整理的C++11 initializer_list 和 Range-based for loop 学习理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 017_python常用小技巧
- 下一篇: 复习支持向量机(SVM)没空看书时,掌握