c++中pass by value 和 pass by reference的疑问

ybz_c 2014-04-29 04:43:43
《Effective C++》条款一里说:对内置(也就是C-like)类型而言 pass-by-value通常比pass-by-reference更高效,
这一点很不理解,难道pass-by-reference不是永远比pass-by-value高效吗? 求大神解答
...全文
198 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ybz_c 2014-04-29
  • 打赏
  • 举报
回复
引用 1 楼 TheNewIpad 的回复:
看看汇编就知道了。 内置类型在按值传参时,只是将变量的值传递到栈上。 然后被调用函数将值取出后,使用即可。 在按引用传参时,需要将被引用的变量的地址压栈, 然后被调用函数首先取出地址,然后再次根据地址寻址获取值。 相互比较 1、 按值传参少了一个取值和寻址过程,效率较高 2、 按值传参由于传递的仅仅是实参的值,因此不能在函数内修改实参的值。 有舍有得。 看场景了。 而用户自定义类型相反 按值传参的话,面临的情况较多 1、 POD类型,按值传参时需要在栈上新建一个等大小的区域,然后按位复制。 然后将栈上新变量的首地址压栈。 相比内置类型,多了好多操作。 2、 有复制控制的类型,哪别说了先在栈上开一个临时的空间,用于保存值,然后调用复制构造函数。 然后将首地址压栈。 不过怎么说, 自定义类型按值传参的话,效率低很多。 而自定义类型按引用传参的话,只是将实参的地址压栈即可。 快捷很多。
懂了 谢谢
dbzhang800 2014-04-29
  • 打赏
  • 举报
回复
你可以类比: 值/对象 ==》 网络上一篇帖子的内容 引用 ==》 帖子的网址 当帖子的内容比网址还要短的时候,你觉得直接告诉别人内容简单,还是网址简单?
TheNewIpad 2014-04-29
  • 打赏
  • 举报
回复
看看汇编就知道了。 内置类型在按值传参时,只是将变量的值传递到栈上。 然后被调用函数将值取出后,使用即可。 在按引用传参时,需要将被引用的变量的地址压栈, 然后被调用函数首先取出地址,然后再次根据地址寻址获取值。 相互比较 1、 按值传参少了一个取值和寻址过程,效率较高 2、 按值传参由于传递的仅仅是实参的值,因此不能在函数内修改实参的值。 有舍有得。 看场景了。 而用户自定义类型相反 按值传参的话,面临的情况较多 1、 POD类型,按值传参时需要在栈上新建一个等大小的区域,然后按位复制。 然后将栈上新变量的首地址压栈。 相比内置类型,多了好多操作。 2、 有复制控制的类型,哪别说了先在栈上开一个临时的空间,用于保存值,然后调用复制构造函数。 然后将首地址压栈。 不过怎么说, 自定义类型按值传参的话,效率低很多。 而自定义类型按引用传参的话,只是将实参的地址压栈即可。 快捷很多。
Gimpel.Software.PC-lint.9.0a 新版本加入了对VS2005 VS2008 SourceInsight Eclipse的支持。真正的9.0版本,Enjoy it! -------------------------------------------------- --- Manual Additions/Modifications --- -------------------------------------------------- PC-lint for C/C++ Version 9.00a This readme.txt supplements the on-line PC-lint manual entitled "Reference Manual for PC-lint/Flexelint" found in the installation directory under the name "pc-lint.pdf" ------ Printing the Reference Manual ------ You have permission to print out the Reference Manual (or other related documentation) in whole or in part in support of the use of this software. ------ Support for Microsoft through Visual Studio 2008 ------ We currently support Microsoft Visual C/C++ 9.0 (as well as all earlier versions of the Microsoft compiler series). Support comes mainly from the compiler options files (co-...lnt). For example options files co-msc70.lnt, co-msc71.lnt, co-msc80.lnt and co-msc90.lnt support versions 7.0, 7.1, 8.0 and 9.0 respectively. ------ What's New ------ To find out what we've added to the product since Version 8.00, check out Chapter 20 "What's New" in the Reference Manual. ------ Front End ------ Your linting experience will be considerably enhanced by adapting your favorite editor or compiler environment to the task of sequencing from error to error. See Section 3.5 of the Reference Manual. ------ Multiple Passes ------ By default, PC-lint/FlexeLint will go through all your modules in one pass. For projects not previously linted there will be enough messages to look at. However, with just one pass, you will not be taking full advantage of our new static data tracking or the interfunction value tracking. With just one pass, we cannot say very much about static variables. We may see a variable being set to a value but we don't know of intervening functions that may be modifying the variable. We will also not know about dangerous return values for functions that are defined later than they are called, and we will not know about dangerous arguments for functions that are defined early. To introduce a second pass you need only to add the command line option: -passes(2) or, if this syntax presents a problem with your Shell, you may use: -passes[2] or, in some cases, -passes=2 is needed. Of course, you can replace the 2 with any number you wish. The larger the number, the more bugs that can be found and the more time that will be required. See Section 10.2 "Value Tracking" in the Reference Manual. Gimpel Software September, 2008

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧