引用是否占用C ++的内存?

编程入门 行业动态 更新时间:2024-10-27 09:42:13
本文介绍了引用是否占用C ++的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经读过很多关于这个问题的文章,从中我得到的引用只是别名,它们不占用任何内存.编译器将引用替换为指向变量的地址.

I have read many articles on this question, from which I get that references are only aliases and they don't consume any memory. Compiler replace the reference with the address of pointing variable.

任何人都可以解释下面的示例将发生什么情况.编译器如何与参考ri一起使用?

Can any one explain what will happen for below example. How compiler work with reference ri?

int main() { int *pi = new int(50); int &ri = *pi; ri = 30; cout << "val = " << ri << " , " << *pi << endl; }

它给了我输出:

val = 30 , 30

推荐答案

int *pi = new int(50); +----+ pi --> | 50 | +----+ int &ri = *pi; +----+ pi --> | 50 | ri = 50, same as *pi, which is 50 +----+ ri = 30; now the contents of what pi points to i.e. *pi is replaced with 30 +----+ pi --> | 30 | +----+

更多推荐

引用是否占用C ++的内存?

本文发布于:2023-11-09 23:15:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!