【转】左值与右值

出处:http://www.embedded.com/electronics-blogs/programming-pointers/4023341/Lvalues-and-Rvalues

C and C++ enforce subtle differences on the expressions to the left and right of the assignment operator

If you've been programming in either C or C++ for a while, it's likely that you've heard the terms lvalue (pronounced "ELL-value") and rvalue (pronounced "AR-value"), if only because they occasionally appear in compiler error messages. There's also a good chance that you have only a vague understanding of what they are. If so, it's not your fault.

Most books on C or C++ do not explain lvalues and rvalues very well. (I looked in a dozen books and couldn't find one explanation I liked.) This may be due to of the lack of a consistent definition even among the language standards. The 1999 C Standard defines lvalue differently from the 1989 C Standard, and each of those definitions is different from the one in the C++ Standard. And none of the standards is clear.

Given the disparity in the definitions for lvalue and rvalue among the language standards, I'm not prepared to offer precise definitions. However, I can explain the underlying concepts common to the standards.

As is often the case with discussions of esoteric language concepts, it's reasonable for you to ask why you should care. Admittedly, if you program only in C, you can get by without understanding what lvalues and rvalues really are. Many programmers do. But understanding lvalues and rvalues provides valuable insights into the behavior of built-in operators and the code compilers generate to execute those operators. If you program in C++, understanding the built-in operators is essential background for writing well-behaved overloaded operators.

Basic concepts
Kernighan and Ritchie coined the term lvalue to distinguish certain expressions from others. In The C Programming Language (Prentice-Hall, 1988), they wrote "An object is a manipulatable region of storage; an lvalue is an expression referring to an object....The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression."

In other words, the left and right operands of an assignment expression are themselves expressions. For the assignment to be valid, the left operand must refer to an object-it must be an lvalue. The right operand can be any expression. It need not be an lvalue. For example:

int n;

declares n as an object of type int. When you use n in an assignment expression such as:

n = 3;

n is an expression (a subexpression of the assignment expression) referring to an intobject. The expression n is an lvalue.

Suppose you switch the left and right operands around:

3 = n;

Unless you're a former Fortran programmer, this is obviously a silly thing to do. The assignment is trying to change the value of an integer constant. Fortunately, C and C++ compilers reject it as an error. The basis for the rejection is that, although the assignment's left operand 3 is an expression, it's not an lvalue. It's an rvalue. It doesn't refer to an object; it just represents a value.

I don't know where the term rvalue comes from. Neither edition of the C Standard uses it, other than in a footnote stating "What is sometimes called 'rvalue' is in this standard described as the 'value of an expression.'"

The C++ Standard does use the term rvalue, defining it indirectly with this sentence: "Every expression is either an lvalue or an rvalue." So an rvalue is any expression that is not an lvalue.

Numeric literals, such as 3 and 3.14159, are rvalues. So are character literals, such as'a'. An identifier that refers to an object is an lvalue, but an identifier that names an enumeration constant is an rvalue. For example:

enum color { red, green, blue };
color c;
...
c = green;    // ok
blue = green;    // error

The second assignment is an error because blue is an rvalue.

Although you can't use an rvalue as an lvalue, you can use an lvalue as an rvalue. For example, given:

int m, n;

you can assign the value in n to the object designated by m using:

m = n;

This assignment uses the lvalue expression n as an rvalue. Strictly speaking, a compiler performs what the C++ Standard calls an lvalue-to-rvalue conversion to obtain the value stored in the object to which n refers.

Lvalues in other expressions
Although lvalues and rvalues got their names from their roles in assignment expressions, the concepts apply in all expressions, even those involving other built-in operators.

For example, both operands of the built-in binary operator + must be expressions. Obviously, those expressions must have suitable types. After conversions, both expressions must have the same arithmetic type, or one expression must have a pointer type and the other must have an integer type. But either operand can be either an lvalue or an rvalue. Thus, both x + 2 and 2 + x are valid expressions.

Although the operands of a binary + operator may be lvalues, the result is always an rvalue. For example, given integer objects m and n:

m + 1 = n;

is an error. The + operator has higher precedence than the = operator. Thus, the assignment expression is equivalent to:

(m + 1) = n;    // error

which is an error because m + 1 is an rvalue.

As another example, the unary & (address-of) operator requires an lvalue as its operand. That is, &n is a valid expression only if n is an lvalue. Thus, an expression such as &3 is an error. Again, 3 does not refer to an object, so it's not addressable.

Although the unary & requires an lvalue as its operand, it's result is an rvalue. For example:

int n, *p;
...
p = &n;    // ok
&n = p;    // error: &n is an rvalue

In contrast to unary &, unary * produces an lvalue as its result. A non-null pointer p always points to an object, so *p is an lvalue. For example:

int a[N];
int *p = a;
...
*p = 3;     // ok

Although the result is an lvalue, the operand can be an rvalue, as in:

*(p + 1) = 4;    // ok

Data storage for rvalue
Conceptually, an rvalue is just a value; it doesn't refer to an object. In practice, it's not that an rvalue can't refer to an object. It's just that an rvalue doesn't necessarily refer to an object. Therefore, both C and C++ insist that you program as if rvalues don't refer to objects.

The assumption that rvalues do not refer to objects gives C and C++ compilers considerable freedom in generating code for rvalue expressions. Consider an assignment such as:

n = 1;

where n is an int. A compiler might generate named data storage initialized with the value 1, as if 1 were an lvalue. It would then generate code to copy from that initialized storage to the storage allocated for n. In assembly language, this might look like:

one: .word 1
...
mov (one), n

Many machines provide instructions with immediate operand addressing, in which the source operand can be part of the instruction rather than separate data. In assembly, this might look like:

mov #1, n

In this case, the rvalue 1 never appears as an object in the data space. Rather, it appears as part of an instruction in the code space.

On some machines, the fastest way to put the value 1 into an object is to clear it and then increment it, as in:

clr n
inc n

Clearing the object sets it to zero. Incrementing adds one. Yet data representing the values 0 and 1 appear nowhere in the object code.

More to come
Although it's true that rvalues in C do not refer to objects, it's not so in C++. In C++, rvalues of a class type do refer to objects, but they still aren't lvalues. Thus, everything I've said thus far about rvalues is true as long as we're not dealing with rvalues of a class type.

Although lvalues do designate objects, not all lvalues can appear as the left operand of an assignment. I'll pick up with this in my next column.

 

转载于:https://www.cnblogs.com/csuftzzk/archive/2013/04/26/3045122.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378514.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Opencv将处理后的视频保存出现的问题

问题描述: 代码运行过程中,imshow出来的每帧的效果图是正确的,但是按照网上的方法保存下来却是0kb,打开不了。 参考的网上的一些方法,均是失败的,具体原因我也不清楚: 1、例如我这样设置&#x…

Java Number shortValue()方法与示例

Number类shortValue()方法 (Number Class shortValue() method) shortValue() method is available in java.lang package. shortValue()方法在java.lang包中可用。 shortValue() method is used to return the value denoted by this Number object converted to type short (…

MATLAB可以打开gms文件吗,gms文件扩展名,gms文件怎么打开?

.gms文件类型:Gesture and Motion Signal File扩展名为.gms的文件是一个数据文件。文件说明:Low-level, binary, minimal but generic format used to organize and store Gesture and Motion Signals in a flexible and optimized way; gesture-related…

黑白图片颜色反转并保存

将图像的黑白颜色反转并保存 import cv2 # opencv读取图像 img cv2.imread(rE:\Python-workspace\OpenCV\OpenCV/YY.png, 1) cv2.imshow(img, img) img_shape img.shape # 图像大小(565, 650, 3) print(img_shape) h img_shape[0] w img_shape[1] # 彩色图像转换为灰度图…

家猫WEB系统

现在只放源码在些.为它写应用很简单有空整理文档演示地址:jiamaocode.com/os/ 源码:http://jiamaocode.com/ProCts/2011/04/14/1918/1918.html转载于:https://www.cnblogs.com/jiamao/archive/2011/04/16/2018339.html

C# DataRow数组转换为DataTable

public DataTable ToDataTable(DataRow[] rows) { if (rows null || rows.Length 0) return null; DataTable tmp rows[0].Table.Clone(); // 复制DataRow的表结构 foreach (DataRow row in rows) tmp.Rows.Add(row); // 将DataRow添加…

plesk 运行不了php,如何在Plesk中使用composer(使用其他版本的PHP运行Composer)

对于基于Plesk的服务器, composer的默认安装将使用系统安装的PHP版本, 而不使用Plesk所安装的任何版本。尽管Composer至少需要PHP 5.3.2, 但是当你尝试在需要特定版本PHP的项目中安装依赖项时, 就会出现问题。例如, 如果你有一个至少需要PHP 7.2的项目, 并且系统的默认PHP安装是…

Java Calendar hashCode()方法与示例

日历类hashCode()方法 (Calendar Class hashCode() method) hashCode() method is available in java.util package. hashCode()方法在java.util包中可用。 hashCode() method is used to retrieve the hash code value of this Calendar. hashCode()方法用于检索此Calendar的哈…

Error: Flash Download failed - Target DLL has been cancelled

博主联系方式: QQ:1540984562 QQ交流群:892023501 群里会有往届的smarters和电赛选手,群里也会不时分享一些有用的资料,有问题可以在群里多问问。 由于换了新电脑,keil重装了下,然而之前的MCU的支持包没有安装,以及一些其他的问题,导致可以编译但是不能将程序烧录到单片…

设计一个较为合理的实验方案来研究芳纶纤维的染色热力学性能

请你设计一个较为合理的实验方案来研究芳纶纤维的染色热力学性能?包括吸附等温线、亲和力、染色热和染色熵的测定,并指出实验中应注意哪些事项来减少实验误差? 标准答案: 染色热力学理论研究染色平衡问题。研究染色热力学性能:首先研究选择适宜的染料 吸附等温线类型测定…

我也谈委托与事件

虽然在博客园里面已经有很多关于C#委托和事件的文章,但是为了自己在学习的过程中,加深对委托的理解,我还是决定写一下自己的心得体会。以备他日在回来复习。委托(delegate)是一个类,但是这个类在声明的时候…

php错误拦截机制,php拦截异常怎么写-PHP问题

php拦截异常可以通过PHP的错误、异常机制及其内建数set_exception_handler、set_error_handler、register_shutdown_function 来写。首先我们定义错误拦截类,该类用于将错误、异常拦截下来,用我们自己定义的处理方式进行处理,该类放在文件名为…

智能车复工日记【4】:关于图像的上下位机的调整问题总结

系列文章 【智能车Code review】—曲率计算、最小二乘法拟合 【智能车Code review】——坡道图像与控制处理 【智能车Code review】——拐点的寻找 【智能车Code review】——小S与中S道路判断 【智能车Code review】——环岛的判定与补线操作 智能车复工日记【1】——菜单索引…

设计合理的实验方案来研究阳离子改性棉织物与未改性棉的染色动力学性能

染色动力学性能研究染色的什么问题?设计合理的实验方案来研究阳离子改性棉织物与未改性棉的染色动力学性能?并指出如何计算反映染色动力学的主要参数? 标准答案: 染色动力学研究染色速率问题。 为了研究阳离子改性棉纤维及未改性棉纤维对活性染料染色动力学性能,首先要测…

Java ArrayList toArray()方法及示例

ArrayList类的toArray()方法 (ArrayList Class toArray() method) Syntax: 句法: public Object[] toArray();public T[] toArray(T[] elements);toArray() method is available in java.util package. toArray()方法在java.util包中可用。 toArray() method is us…

小练习:新闻网站、SNS网站图片播放器jQuery版

新闻网站和sns网站常见的图片浏览器。直接看效果吧,效果预览:http://u.vps168.com.cn/cos2004/photoView/, ie7、ie8、ff预览正常,ie6和chrome预览有一点小问题,研究中 作者:绿色花园 出处:htt…

SQL SERVER的锁机制(四)——概述(各种事务隔离级别发生的影响)

SQL SERVER的锁机制(一)——概述(锁的种类与范围) SQL SERVER的锁机制(二)——概述(锁的兼容性与可以锁定的资源) 本文上接SQL SERVER的锁机制(三)——概述&a…

如何测定拼色染液中,各染料在不同染色时间时,染液中残留染料量及织物上吸附上染的染料量?

如何测定拼色染液中,各染料在不同染色时间时,染液中残留染料量及织物上吸附上染的染料量? 标准答案: 通常依据各染料对特定波长光的吸光度具有加和性;吸光度值符合朗伯比尔定律,吸光度与浓度成正比;吸光度值分光光度计可以读出;通过联立方程,可以求出拼色各染料在染液中…

oracle云数据库 免费的吗,使用免费的Oracle云服务-创建ATP数据库

前面我们讲了在Oracle的云主机安装数据库,虽然现在安装数据库已经很简单了,但是真正要在生产环境使用,还是需要进行很多配置和优化的,这通常需要专业的人员。而ATP(自治事务性数据库服务)正是Oracle云提供的非常强大的数据库云服务…

golang判断结构体为空_如何在Golang中检查结构是否为空?

golang判断结构体为空The size of an empty structure is zero in Golang. Here, empty structure means, there is no field in the structure. 在Golang中, 空结构的大小为零。 在此, 空结构表示该结构中没有字段。 Eg: 例如: Type struc…