c ++中的右值如何存储在内存中? [英] How are rvalues in c++ stored in memory?

查看:1208
本文介绍了c ++中的右值如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习lvaluesrvalues和它们的内存分配.因此,在学习材料很多的情况下,会有一些混乱.

Trying to learn lvalues, rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos.

rvalue是仅需要在创建它的表达式的边界中存在的值(至少到C ++ 11为止).因此,它具有一个地址和占用的内存块.但是根据定义,我们无法获得rvalue的地址,因为与lvalue相比,它是一个临时对象. 但是即使在C ++ 11之前,我们也可以通过从函数返回rvalue并将其保存为const引用类型(嗯,我想不是地址,而是一个值)来获取地址.

An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it has an address and block of memory that it occupies. But by definition we cannot get an address of rvalue, because it is a temporary object in contrast to an lvalue. But even before C++11 we were able to get an address of rvalue by returning it from a function and saving it into a const reference type (uh, I guess not an address but a value).

那么,更准确地说,rvalue分配如何工作?程序或OS真正记得多长时间创建rvalue并将其标记为已分配的内存,而另一个对象不能代替它?

So, more precisely, how does rvalue allocation work? For how long does the program or OS really remember that place of memory where the rvalue was created and marked as allocated and another object cannot take its place?

我怎么看,现在rvalues的存储方式与lvalues相同,但我们只是拥有访问它们的其他权利.它们还有其他类型的释放方式-对于lvalues超出范围,对于rvalues可以通过存在表达式边界或直到没有更多链接来优化.

How I see it, now rvalues are stored just like lvalues but we simply have other rights to access them. And they have other types of deallocation - for lvalues going out of scope, for rvalues may be optimized by existence in bounds of expression or until there is no more links to it.

推荐答案

简短的答案:它取决于实现.

Short answer: it's implementation dependent.

其背后的主要原因是,一如既往的自由使编译器可以提高代码的性能.理解这一点的一种更具体的方法是记住,一个值可以存储在CPU的寄存器中,而从不实际存在于内存中,这或多或少意味着该值没有地址.我不会打赌我拥有的一切,但这可能是我们无法获得右值的地址"的主要原因之一.

The main reasoning behind this is as always freedom for the compiler to improve performances of your code. A more concrete way to understand this is to remember that a value can be stored in a register of your CPU and never actually be in your memory which more or less means that the value has no address. I won't bet everything i have on it but this is probably one of the main reasons why "we cannot get an address of an rvalue".

以更一般的方式,由于右值在语义上是临时的,因此它更有可能被放置在临时位置或以无法轻易映射到地址的方式进行优化,即使它可以在术语上适得其反性能.

In a more general way since an rvalue is semantically temporary it is more likely to be put in temporary places or optimised in a way where it cannot easily be mapped to an address and even if it can that would be counter productive in terms of performance.

这篇关于c ++中的右值如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆