Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2622207
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: LINUX

2014-04-27 20:38:42


http://www.cppblog.com/cc/archive/2006/05/25/7619.html

左值和右值

我们在看书的时候,经常可以看到关于 左值(L-value) 和 右值(R-value) 的概念,那么到底什么是左值,什么是右值,它们之间的区别又是在哪里呢?

    通俗的讲,左值就是能够出现在赋值符号左面的东西,而右值就是那些可以出现在赋值符号右面的东西了。

举个很简单的例子:

a=b+100;

那么这里a就是左值,b+25就是一个右值。左值和右值之间是不一定都能互换的,上面的这个例子就是不能互换的,如果写成

b+100=a;

大家都能看出来这样写会不编译通过的,因为编译器无法判断b+100的内存地址,所以不能操作。

看了这个例子,可以做一个总结,左值必须应该是一个变量或者是表达式等,但是它的物理位置是可以确定的,而右值不一定,这也是它们两者之间的区别。

关于左值是表达式的例子有数组,还有指针这些都可以。

int array[10];

int a=5;

array[a+3]=10; //这里左值就是一个数组表达式了

 

此文完。

 

 另外:朋友给了一些其他方面的提示 (左值右值翻译可能有些问题)

错了,没有什么左值和右值!
翻译害人不浅,
L-value中的L指的是Location,表示可寻址。The "l" in lvalue can be though of as location
R-value中的R指的是Read,表示可读。The "r" in rvalue can be thought of as "read" value.  


是否占用内存, 是否存在于符号表中, 是2个正交的概念  


xxx.c

int global_variable;       // 占用内存, 且符号表中存在
int global_array[1212]; // 占用内存, 且符号表中存在

void  ...


如果local_array不存在symbol table中,编译器会把它放在哪里呢?

是否占用内存, 是否存在于符号表中, 是2个正交的概念  


xxx.c

int global_variable;       // 占用内存, 且符号表中存在
int global_array[1212]; // 占用内存, 且符号表中存在

void f(void)
{
      int local_variable;       // 占用内存, 符号表中不存在
      int local_array[1212]; // 占用内存, 符号表中不存在
}

http://www.cnblogs.com/SelaSelah/archive/2012/05/12/2497686.html

左值(LValue)和右值(RValue)的一个快捷记法是赋值运算,左值是赋值运算左边的值,右值就是右边(=,=废话)。例如:

int a = 5;

a就是左值,5就是右值。

 

当然,如果真是这么个含义,那么这概念就相当蛋疼了。其实不是这样的~~

 

左值其实是对一块内存区域的引用(这个还不是C++中的int &a之类的引用),比如上边的a,就对应了一块内存区域(起始地址为&a,大小为sizeof(int))。

更专业的定义在这里:

An object is a region of storage that can be examined and stored into. An lvalue is an expression that refers to such an object. An lvalue does not necessarily permit modification of the object it designates. For example, a const object is an lvalue that cannot be modified.

右值对应的玩意其实也在内存里,但是我们忽略这一点,认为它存在于冥冥之中。例如上边那个5,其实它在静态数据段或者程序二级制代码中,但我们不关心这个,认为它无法修改。

 

附个链接:

%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05lvalue.htm



评论

# re: 左值和右值 2007-09-07 08:51 
那么对于(a=2)=66这样的表达式怎么看?另外,为什么自增(减)运算只能用于左值,不能用于右值,即表达式++(a++)为什么错误呢?  回复  更多评论 
  

# re: 左值和右值 2007-09-07 17:27 JetSun
@wu 
(a=2)=66 中就是按顺序赋值就是了 所以a=66 

++(a++) 
至于右值为什么不能用,因为后增量(后减量)返回的值是修改前的变量值,故不为左值,所以他就不能用了  回复  更多评论 
  

# re: 左值和右值 2008-06-07 19:28 
++(a++) 
a++相当于 
int a; 

int temp=a; 
a++; 
teturn temp; 

所以我们可以将++(a++)看成++temp;而temp 
显然是一个右值,所以不能用啊~~  回复  更多评论 
  

# re: 左值和右值 2008-09-01 18:29 
错了,没有什么左值和右值!
翻译害人不浅,
L-value中的L指的是Location,表示可寻址。The "l" in lvalue can be though of as location
R-value中的R指的是Read,表示可读。The "r" in rvalue can be thought of as "read" value.  回复  更多评论 
  

# re: 左值和右值 2008-09-23 15:59 
原创好文章,谢谢楼主。  回复  更多评论 
  

# re: 左值和右值[未登录] 2009-02-14 07:25 
From Wikipedia, the free encyclopedia 
Jump to: navigation, search 
L-value or L value may refer to: 

A value (computer science) that has an address 
The value assigned to an L-shell, a particular set of planetary magnetic field lines 
A measure of brightness of a lunar eclipse on the Danjon scale 

From Wikipedia, the free encyclopedia 
Jump to: navigation, search 
R-value can refer to: 

Properties of materials: 
R-value (insulation), the efficiency of insulation 
R-value (soils), stability of soils and aggregates for pavement construction 
r-value, in computer science, a value that does not have an address in a computer language 
R-factor (crystallography), a measure of the agreement between the crystallographic model and the diffraction data 
In statistics, r (or r value) refers to the Pearson product-moment correlation coefficient, often called simply correlation coefficient 

翻译不对,谢谢  回复  更多评论 
  

# re: 左值和右值 2009-02-20 10:37 
这是中的一部分:

Lvalues and Rvalues
左值和右值
We'll have more to say about expressions in Chapter 5, but for now it is useful to know that there are two kinds of expressions in C++:

我们在第五章再详细探讨表达式,现在先介绍 C++ 的两种表达式:

lvalue (pronounced "ell-value"): An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

左值(发音为 ell-value):左值可以出现在赋值语句的左边或右边。

rvalue (pronounced "are-value"): An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

右值(发音为 are-value):右值只能出现在赋值的右边,不能出现在赋值语句的左边。

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned. Given the variables:

变量是左值,因此可以出现在赋值语句的左边。数字字面值是右值,因此不能被赋值。给定以下变量:

int units_sold = 0;
double sales_price = 0, total_revenue = 0;



it is a compile-time error to write either of the following:

下列两条语句都会产生编译错误:

// error: arithmetic expression is not an lvalue
units_sold * sales_price = total_revenue;
// error: literal constant is not an lvalue
0 = 1;



Some operators, such as assignment, require that one of their operands be an lvalue. As a result, lvalues can be used in more contexts than can rvalues. The context in which an lvalue appears determines how it is used. For example, in the expression

有些操作符,比如赋值,要求其中的一个操作数必须是左值。结果,可以使用左值的上下文比右值更广。左值出现的上下文决定了左值是如何使用的。例如,表达式

units_sold = units_sold + 1;

the variable units_sold is used as the operand to two different operators. The + operator cares only about the values of its operands. The value of a variable is the value currently stored in the memory associated with that variable. The effect of the addition is to fetch that value and add one to it.

中,units_sold 变量被用作两种不同操作符的操作数。+ 操作符仅关心其操作数的值。变量的值是当前存储在和该变量相关联的内存中的值。加法操作符的作用是取得变量的值并加 1。

The variable units_sold is also used as the left-hand side of 

可以看出,左值和右值是跟左和右挂钩的。  回复  更多评论 
  

# re: 左值和右值 2009-09-16 21:12 
++(a++) 
a++ return a const object we can say it is b. so ++b is wrong  回复  更多评论 
  

# re: 左值和右值 2010-01-05 00:32 
@whtank
高度啊 顶  回复  更多评论 
  

# re: 左值和右值 2010-04-13 14:22 溪流
l, r 的原始含义真的这样啊?刚才同事跟我说我还不信呢。。。  回复  更多评论 
  

# re: 左值和右值 2010-05-27 16:19 
msdn上的,左值这种翻译并无差错。

An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.  回复  更多评论 
  

# re: 左值和右值 2010-07-21 21:42 
我认为左值右值才是正确的  回复  更多评论 
  

# re: 左值和右值 2010-07-22 17:52 
感谢楼主。。l-value r-value 真神秘  回复  更多评论 
  

# re: 左值和右值 2010-08-08 17:04 
Expressions that refer to memory locations are called "l-value" expressions. An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers. 

The term "r-value" is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values. 

  回复  更多评论 
  

# re: 左值和右值 2010-08-08 17:08 
应该是先有了l-value这个概念,r-value只是起到与lvalue对比的作用。l-value含有左的意思,但不仅仅是左的意思,r-value只有右的意思  回复  更多评论 
  

# re: 左值和右值 2010-12-20 10:42 
@wu
不对吧,我记得赋值作为副产品的返回是R值,不是地址,你的第一个表达式就不对啊  回复  更多评论 
  

# re: 左值和右值 2011-08-12 16:19 
感谢楼主的分享。

关于左值和右值,Dan Saks发表过一篇题为Lvalues and Rvalues的文章,阅读之感觉写的很好,概念一下清晰很多,顺便翻译了一下。有兴趣的朋友可以交流学习一下。
译文:
原文:http://www.eetimes.com/discussion/programming-pointers/4023341/Lvalues-and-Rvalues  回复  更多评论 
  

# re: 左值和右值 2011-08-16 12:07 
@KevinKitty

不好意思,译文链接给错了,应该是:  回复  更多评论 
  

# re: 左值和右值 2012-03-07 20:52 
@Nash
a++相当于
int a;
{
int temp=a;
a++;
teturn temp;


用a++,解释a++.呵呵,太有才了
写成a=a+1.更好理解吧  回复  更多评论 
  

# re: 左值和右值 2012-03-09 12:32 JetSun
谢谢讨论 这样百家争鸣 挺好@jie
  回复  更多评论 
  

# re: 左值和右值[未登录] 2012-03-22 11:20 
看了楼上给的原文,感觉判断lvalue和rvalue的方法就是看if it is refer to an object。因为原文中有这样一句An object is a manipulatable region of storage; an lvalue is an expression referring to an object....  回复  更多评论 
  

# re: 左值和右值 2012-06-27 14:16 
其实我感觉左值和右值的概念吧,可能跟汇编有关。 

比如 MOV A, #300 

其中A就必须是物理地址,或者累加器  回复  更多评论 
阅读(402) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~