为什么右值没有地址?

发布于 2024-12-04 06:52:17 字数 55 浏览 0 评论 0原文

为什么右值没有内存地址?程序执行时它们是否未加载到 RAM 中,还是引用处理器寄存器中存储的值?

Why don't rvalues have a memory address? Are they not loaded into the RAM when the program executes or does it refer to the values stored in processor registers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃不饱 2024-12-11 06:52:17

你的问题(“为什么右值没有内存地址?”)有点困惑。右值是一种表达式。 表达式没有地址:对象有地址。更正确的问题是“为什么不能将取址运算符应用于右值表达式?”

答案相当简单:您只能获取对象的地址,并非所有右值表达式都引用对象(例如,表达式 42 有一个值,但不引用对象) 。

某些右值表达式确实引用对象,但此类对象缺乏持久性。右值表达式引用的对象是一个临时对象,并在创建它的表达式末尾被销毁。这些对象确实有地址(您可以通过调用临时对象的成员函数来轻松发现这一点;this 指针必须指向临时对象,因此临时对象必须有一个地址)。

这是左值表达式和右值表达式之间的根本区别。左值表达式引用具有持久性的对象:左值表达式引用的对象 持续超出单个表达式

Your question ("Why don't rvalues have a memory address?") is a bit confused. An rvalue is a kind of expression. Expressions don't have addresses: objects have addresses. It would be more correct to ask "why can one not apply the address-of operator to an rvalue expression?"

The answer to that is rather simple: you can only take the address of an object and not all rvalue expressions refer to objects (for example, the expression 42 has a value but does not refer to an object).

Some rvalue expressions do refer to objects, but such objects lack persistence. An object referred to by an rvalue expression is a temporary object and is destroyed at the end of the expression in which it is created. Such objects do indeed have addresses (you can easily discover this by calling a member function on a temporary object; the this pointer must point to the temporary object and thus the temporary object must have an address).

This is the fundamental difference between lvalue expressions and rvalue expressions. Lvalue expressions refer to objects that have persistence: the object to which an lvalue expression refers persists beyond a single expression.

说谎友 2024-12-11 06:52:17

右值视为表达式本身没有地址。但表达式中涉及的对象确实有地址。您可以获取对象的地址,即使它是临时对象。

考虑一下,

const int & i = 10; //ok

这里,10 是一个右值,因此看起来 &i10 的地址>。不,那是错误的。 &iint 类型的临时对象的地址,它是根据表达式 10 创建的。由于临时对象无法绑定到非常量引用,因此我使用 const。这意味着,以下内容是错误的:

int & i = 10; //error

Think of rvalue as value of an expression. The value itself doesn't have address. But the objects involve in the expression do have address. You can take address of an object, even be it a temporary object.

Consider this,

const int & i = 10; //ok

Here, 10 is an rvalue, so it appears that &i is an address of the 10. No, that is wrong. &i is an address of the temporary object of type int, which is created out of the expression 10. And since the temporary object cannot be bound to non-const reference, I use const. That means, the following is an error:

int & i = 10; //error
请恋爱 2024-12-11 06:52:17

该问题混合了与“规范”和“实施”相关的两个不同方面。

“规范”定义了一些抽象规则,这些规则定义了语言如何对待与其一起工作的抽象机器。
将“抽象机器”适应其下的“真实机器”,这是编译器(而不是语言)的目的。

该规范强制执行的是,从语言的角度来看,“存储”(一块具有正确地址的内存)仅提供给具有名称的对象(因为存在命名为 live 的范围)或通过显式请求动态分配的范围(new)。
其他一切都是“临时的”:像对象一样分配、复制和移动,但不需要存在于定义良好且稳定的位置。至少,不是为了语言的目的。

当然,它必须(物理上)保留在某个地方,因此您可以通过适当的转换或转换来尝试猜测内存地址。但是,如果您尝试主动使用它,语言规范不会授予任何一致的行为。
这意味着不同的编译器可以有不同的行为,并且可以根据其目标的真实机器进行更好的优化。

The question mixes two different aspects related to the "specification" and to the "implementation".

The "specification" define some abstract rules that defines how the language behave respect to an abstract machine it works with.
Adapt that "abstract machine" to the "real one" under it its a purpose of the compiler (not the language).

What the specification is enforcing is that -by the language stand point- a "storage" (a piece of memory with a proper address) is given only to objects that have a name (for the existence of the scope that name lives) or that are dynamically allocated with an explicit request (new).
Everything else is "temporary": assign, copy and moves like an object, but is not required to exist in a well defined and stable place. At least, not for the purpose of the language.

Of course, that has to stay (physically) somewhere, so you can -with appropriate casting or conversion- trying to guess a memory address. But the language specification does not grant any consistent behavior if you try to actively use it.
That means that different compilers can behave differently, and optimize the better they can respect to the real machine they target.

喜你已久 2024-12-11 06:52:17

你是什​​么意思,右值确实有一个地址。曾经尝试过

Type const value& = rvalue;
Type const* address = &value;

What do you mean, rvalues do have an address. Ever tried

Type const value& = rvalue;
Type const* address = &value;
伤痕我心 2024-12-11 06:52:17

只需将这种情况

int a = 1 + 2;

1+2 解析为 3。

问问自己:

  • 3 是一个对象吗?
  • 3 将位于内存中的什么位置?

当您需要对象的地址时,可以使用 &。

如果右值是可寻址的,则意味着您可以声明一个指向计算机决定存储位置的指针 3

int* a = &3;

这看起来正确吗? :)

Simply take this case

int a = 1 + 2;

1+2 gets resolved to 3.

Ask yourself:

  • Is 3 an object?
  • Where would 3 be located in memory?

When you need the address of an object, you use &.

If rvalues would be addressable, it means you could declare a pointer to where your computer decided to store 3

int* a = &3;

Does that seem right? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文