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) 这段内存是5个整形(20个字节),分别赋值为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
// 创建allocator对象,供后面vector构造使用,地址为 ebp-111h
00933FC4 lea ecx,[ebp-111h]
00933FCA call std::allocator<int>::allocator<int> (0931163h)
00933FCF push eax
// std::initializer_list<int> 构造
// 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 构造
// vector(initializer_list<_Ty> _Ilist, const _Allo