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 _Alloc& _Al = _Alloc())
// _Al参数是在 00933FCF 位置压栈的
// _Ilist参数是按值传递,将成员_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() 结果保存在 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() 结果保存在 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

// 跳转 for 循环的条件比较
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() 比较,如果相等则跳出循环
0093401F  mov         eax,dword ptr [ebp-30h]  
00934022  cmp         eax,dword ptr [ebp-3Ch]  
00934025  je          main+0E2h (0934042h)

// 将 dword ptr [ebp-30h] 迭代器指向的整形数值取出来,放到 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] 取出整形数值,压栈
// 将 "num = %d\n" 压栈
// 调用 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  
    }

// 跳转到迭代器加1位置
00934040  jmp         main+0B6h (0934016h)

return 0;
00934042  mov         dword ptr [ebp-130h],0

// vecotr析构
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

C++11 initializer_list 和 Range-based for loop 学习理解的更多相关文章

  1. Node.js Event Loop 的理解 Timers,process.nextTick()

    写这篇文章的目的是将自己对该文章的理解做一个记录,官方文档链接The Node.js Event Loop, Timers, and process.nextTick() 文章内容可能有错误理解的地方 ...

  2. 11种常用css样式之border学习

    边框border通常简写为"border:1px solid red;"但其实一个完整的border边框其实是由1.border-width/*边框宽度*/,2.border-st ...

  3. C++11新特性——range for

    很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...

  4. C++11中自定义range

    python中的range功能非常好用 for i in range(100): print(i) 现在利用C++11的基于范围的for循环特性实现C++中的range功能 class range { ...

  5. c++11::initializer_list

    #include <initializer_list> template <class T> class initializer_list; initializer_list对 ...

  6. getSelection、range 对象属性,方法理解,解释

    网上转了一圈发现没有selection方面的解释,自己捣鼓下 以这段文字为例子.. <p><b>法国国营铁路公司(SNCF)20日承认,</b>新订购的2000列火 ...

  7. 对Node.JS的事件轮询(Event Loop)的理解

    title: Node.JS的事件轮询(event loop)的理解 categories: 理解 tags: Node JS 机制 当我们知道I/O操作和创建新线程的开销是巨大的! 网站延迟的开销 ...

  8. 11个超棒的iOS开发学习网站

    原文:11 Insanely Great iOS Developers Sites 永不止步地向他人学习 我相信,要想从一个"还不错"的人变成一个卓越的人,我们需要不停地向他人学习 ...

  9. 编辑器开发之 Range 范围对象的学习

    写在前面: 网上有各种富文本编辑器,微博分享等操作,这些功能非常实用,他们就是使用 range,selection 对象来实现的,这两个对象偏冷门,不涉及编辑器一般用不到,range 对象是对选区的操 ...

随机推荐

  1. 使用 Azure PowerShell 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager

    以下步骤演示了如何使用 Azure PowerShell 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型. 也可根据需要通过 Az ...

  2. leetcode Ch8-Others

    1. Rotate Image 旋转图像 顺时针旋转90度:先沿水平线翻转,再沿主对角线翻转. 逆时针旋转90度:先沿竖直线翻转,再沿主对角线翻转. 顺时针旋转180度:水平翻转和竖直翻转各一次. 逆 ...

  3. configparser logging collections 模块

    configparser 模块: 这是一个写入的模块就是把你的信息给写入的模块 #这是一个把信息写入example文件内import configparserconfig = configparser ...

  4. SQL连接的分类

    连接的分类 内连接 等值连接(INNER JOIN) 自然连接(NATURAL JOIN) 交叉连接(CROSS JOIN) 不等连接 外连接 左外连接(LEFT OUTER) 右外连接(RIGHT ...

  5. 解决charles中文乱码(附代码)

    1. 将下面的代码保存为一个*.xml的文件 <?xml version='1.0' encoding='UTF-8' ?> <?charles serialisation-vers ...

  6. 异常处理与MiniDump详解(2) 智能指针与C++异常

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 一.   综述 <异常处理与MiniDump详解(1) C++异常>稍 ...

  7. mac 学习笔记

    1.关于launchctl http://zhengwei.name/2011/11/lanunchctl-notes/ 2.php-fpm 默认配置 php-fpm.conf :/etc/php-f ...

  8. Android笔记之 Web Service 基础

    一.Web Service是什么? 就是网络服务.依据W3C的定义,WebServices(Web服务)是一个用于支持网络间不同机器互操作的软件系统,它是一种自包括.自描写叙述和模块化的应用程序,它能 ...

  9. springmvc与Structs2本质区别

    1.前端控制器不同:structs2入口是一个filter过滤器,springmvc入口是一个servlet. 2.设计思想不同: struts2通过在action类中定义成员变量接收请求参数,str ...

  10. selenium + python自动化测试unittest框架学习(七)随机生成姓名

    在自动化测试过程中经常要测试到添加用户的操作,每次都要输入中文,原本是找了十几个中文写成了列表,然后从列表中随机取出填入用户名文本框中,随着测试的增加,发现同名的人搜索出来一大堆,最后在网上找了个随机 ...