青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 10  文章 - 0  trackbacks - 0
<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

靜心學(xué)習(xí),好好珍惜身邊的人。

常用鏈接

留言簿(1)

隨筆分類(lèi)

隨筆檔案

搜索

  •  

積分與排名

  • 積分 - 3920
  • 排名 - 1711

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

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 int object. 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 rvalues
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.

posted on 2008-12-15 10:43 GLORY 閱讀(549) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C/C++ Specification

只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美午夜精品久久久久免费视| 中文亚洲免费| 亚洲一区二区在线免费观看视频| 伊人伊人伊人久久| 亚洲在线国产日韩欧美| 一区二区三区精品视频| 免费看精品久久片| 麻豆乱码国产一区二区三区| 国产精品尤物福利片在线观看| 夜夜爽av福利精品导航| 亚洲精品视频啊美女在线直播| 久久久久在线| 麻豆精品91| 黑人一区二区三区四区五区| 性欧美xxxx大乳国产app| 午夜精品在线| 国产精品日本精品| 亚洲视频 欧洲视频| 亚洲欧美激情精品一区二区| 欧美日韩一二三区| 日韩视频免费在线| 亚洲精品社区| 欧美日韩国产影片| 夜夜爽99久久国产综合精品女不卡 | 国产一区二区成人久久免费影院| 亚洲视频日本| 欧美一区成人| 国产一区二区三区久久精品| 欧美亚洲一区二区在线| 久久久久成人网| 黄色精品一区| 免费亚洲电影| 亚洲狼人综合| 欧美一区二区免费| 国内成人精品一区| 久久亚洲一区二区| 亚洲欧洲三级电影| 中文精品一区二区三区| 国产精品乱码人人做人人爱| 亚洲女性喷水在线观看一区| 久久婷婷国产麻豆91天堂| 在线免费日韩片| 欧美日本中文字幕| 亚洲永久免费观看| 欧美aⅴ99久久黑人专区| 亚洲国产精品精华液2区45| 欧美久久九九| 亚洲欧美日韩在线观看a三区 | 亚洲黄色片网站| 一区二区三区欧美在线| 国产精品外国| 玖玖综合伊人| 亚洲少妇自拍| 欧美1区视频| 亚洲欧美日韩在线一区| 国模套图日韩精品一区二区| 欧美大胆a视频| 午夜精品久久久久久久99樱桃| 久久一区二区三区国产精品| 一区二区三区视频观看| 国产视频在线一区二区 | 国产一区二区精品久久91| 欧美不卡视频一区| 亚洲欧美激情视频在线观看一区二区三区| 久久视频一区| 亚洲综合色噜噜狠狠| 伊人成人网在线看| 国产精品久久久久久久久久久久久久 | 在线一区免费观看| 激情av一区二区| 欧美小视频在线| 免费不卡视频| 欧美一区二区三区四区在线观看 | 一本色道久久综合亚洲精品高清 | 精品福利免费观看| 国产精品国产三级国产专区53 | 在线观看亚洲精品| 国产欧美日韩一区二区三区在线| 女女同性女同一区二区三区91| 亚洲午夜在线观看视频在线| 亚洲高清久久网| 久久午夜羞羞影院免费观看| 午夜免费在线观看精品视频| 日韩一级精品视频在线观看| 在线欧美不卡| 国产在线精品自拍| 国产精品爱啪在线线免费观看| 免费观看国产成人| 久久精品国产99| 香港久久久电影| 亚洲一区二区网站| 国产精品99久久99久久久二8| 欧美激情一区二区| 老牛国产精品一区的观看方式| 篠田优中文在线播放第一区| 亚洲午夜电影| 亚洲少妇最新在线视频| 亚洲伦理自拍| 亚洲精品一区二区三区福利| 亚洲第一中文字幕| 在线观看欧美一区| 一区二区视频在线观看| 激情av一区| 亚洲第一精品在线| 亚洲国产精品久久91精品| 极品av少妇一区二区| 一区二区三区在线观看欧美 | 影音先锋亚洲精品| 激情成人中文字幕| 在线播放中文字幕一区| 狠狠色综合色区| 伊人天天综合| 亚洲精品九九| 一区二区三区高清不卡| 亚洲一区日韩在线| 小嫩嫩精品导航| 欧美主播一区二区三区美女 久久精品人 | 久久久精品视频成人| 久久狠狠亚洲综合| 免费观看成人www动漫视频| 欧美 日韩 国产一区二区在线视频 | 久久天堂成人| 欧美电影免费| 日韩天堂av| 午夜伦欧美伦电影理论片| 欧美在线一级va免费观看| 久久久久久免费| 欧美成人国产一区二区| 欧美日韩精品在线视频| 国产精品免费看| 一区二区视频在线观看| 亚洲精选国产| 欧美一区=区| 老司机免费视频久久| 91久久精品国产| 亚洲综合视频1区| 久久综合一区二区| 欧美婷婷久久| 在线成人激情| 亚洲视频免费看| 另类国产ts人妖高潮视频| 亚洲人成毛片在线播放女女| 亚洲欧美www| 欧美激情精品久久久久久大尺度| 国产精品美女久久久久久2018 | 免费短视频成人日韩| 欧美性猛交视频| 伊人久久男人天堂| 亚洲一级片在线看| 欧美+亚洲+精品+三区| 一区二区三区.www| 久久在线观看视频| 国产精品日韩一区二区| 亚洲激情在线视频| 欧美在线二区| 91久久精品日日躁夜夜躁欧美| 亚洲自拍三区| 欧美精品在线免费| 激情婷婷欧美| 欧美在线视频一区二区| 亚洲精品免费电影| 久久免费视频在线观看| 国产精品私人影院| 一本色道久久综合亚洲精品高清| 久久在线91| 午夜精品成人在线视频| 欧美日韩午夜| 亚洲免费高清| 欧美成人综合网站| 欧美综合国产精品久久丁香| 国产精品蜜臀在线观看| 洋洋av久久久久久久一区| 欧美1区2区| 久久久久成人网| 国产原创一区二区| 欧美一级理论性理论a| 一本色道精品久久一区二区三区| 美女久久一区| 亚洲国产精品va在线看黑人动漫 | 午夜精品短视频| 夜夜夜精品看看| 欧美日韩在线另类| 一个色综合av| 亚洲精选91| 欧美日韩在线播| 国产精品99久久久久久久久| 亚洲激情在线播放| 欧美高清不卡在线| 日韩视频一区二区三区| 亚洲国产高清高潮精品美女| 美国三级日本三级久久99| 亚洲黄网站黄| 亚洲黄色成人网| 欧美色大人视频| 午夜国产精品视频免费体验区| 亚洲视屏在线播放| 国产日韩欧美91| 免费久久99精品国产自在现线| 久久久久久网| 亚洲精品日韩在线|