reference 变量的几个问题
来源:互联网 发布:java教材pdf 编辑:程序博客网 时间:2024/05/06 06:16
问题:
当你声明一个pointer时,系统会给这个变量分配多少内存?
根据OS的位数,32位返回4个字节,64位8个字节
当你声明一个reference变量时,系统会给这个变量分配多少内存?
不分配空间。reference变量和所关联的变量是同一个变量。
int x;
int &ref=x;
cout<<endl<<&x; //address of ‘x’
cout<<endl<<&ref; //address of ‘ref’
Both addresses will be the same (which is whyworking on ‘x’ is the same as working on ‘ref’).
Compiler 会在编译的时候用x来代替ref.
当一个函数用Pass by reference 的时候,compiler是如何实现的?
As forpass-by-reference, if a language has pointers, pass by reference can be done bypassing pointers by value. The compiler can just have a preprocessing stepwhere it "eliminates" pass by reference by making the followingtranslation:
- For every function parameter that is pass by reference, it changes it to a pointer to that type passed by value (e.g. void func(int &foo) -> void func(int *foo))
- For every use of that pass-by-reference parameter inside that function, change it to an explicit pointer dereference (e.g. foo -> *foo) (with the exception that if it is passed by reference again, don't dereference it)
- Every time that function is called, whatever is passed to the pass-by-reference variable, take the address of it explicitly (e.g. func(bar) -> func(&bar))
- reference 变量的几个问题
- Dojo class中跟变量相关的几个问题
- Linux 脚本中使用变量的几个问题
- 关于static静态变量的几个问题
- C语言中指针变量使用时常见的几个问题
- C中的变量及几个问题
- 答初学者的几个问题
- 关于《仙剑》的几个问题
- mysql碰到的几个问题
- 自己设计的几个问题
- 面试的几个问题
- CVSNT/WINCVS的几个问题
- Nokia S60的几个问题
- nmake的几个问题
- 我不明白的几个问题
- 关于CCheckListBox的几个问题!
- 关于网络的几个问题
- 转载BPEL的几个问题
- CentOS上搭建Nginx + Mono 运行 asp.net
- Android 用户界面---广播通知(Toast Notifications)
- powerscript基础学习一
- oracle dp命令的使用说明
- ubuntu 下安装svn客户端
- reference 变量的几个问题
- Java基础02-基本类型相关知识
- TI公司之DSP结构与芯片
- igbinary vs serialize vs json_encode
- 安装oracle11身份证明检索失败
- JS中遍历普通数组和字典数组的区别
- js中判断是否为空
- 一个小小的硕士生的offer路
- 与或运算短路,及