C中的lvalue和rvalue

该贴子第一条回答虽然浅尝辄止,但还是很有参考价值。

https://www.quora.com/What-is-lvalue-and-rvalue-in-C

IBM一个简单的说法是:

“…通俗的左值的定义就是非临时对象,那些可以在多条语句中使用的对象。所有的变量都满足这个定义,在多条代码中都可以使用,都是左值。右值是指临时的对象,它们只在当前的语句中有效。”

https://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/index.html

可以这样理解,“非临时”意味着该值应该存储在系统的寄存器中,而“临时”意味着该值可以存储在内存或临时寄存器中。因为rvalue临时存在于内存中,对rvalue的取地址操作是没有稳定的结果的。

回到quora的回答。

变量是表象,真正有意义的是变量对应的地址。

对于某个变量对应的地址,如果往该地址存一个数值,这是lvalue,因为该值最终需要送到寄存器用于做后续运算;如果从该地址取一个值,这是rvalue,因为取出来的值会被临时存在内存或临时寄存器中。

The difference is when a variable is used as an lvalue, the compiler sets up the computer to store a value to the address in the pointer. When it's used as an rvalue, it tells the computer to load a value from the address in the pointer into a register in preparation for some other operation, like calling a function, or for an indexing operation (for use in an array), or an arithmetic computation.

注意该段文字专门强调了store和load这两个命令。