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

隨筆 - 10  文章 - 0  trackbacks - 0
<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

靜心學習,好好珍惜身邊的人。

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

搜索

  •  

積分與排名

  • 積分 - 3885
  • 排名 - 1714

最新評論

閱讀排行榜

評論排行榜

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 閱讀(544) 評論(0)  編輯 收藏 引用 所屬分類: C/C++ Specification
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久中文在线| 欧美一级片一区| 欧美在线视频a| 亚洲人成网站999久久久综合| 日韩亚洲一区在线播放| 欧美大片在线影院| 欧美在线一二三| 亚洲一二三区精品| 亚洲视频狠狠| 妖精视频成人观看www| 亚洲第一网站免费视频| 老司机午夜精品视频| 亚洲久久成人| 国产精品主播| 国产精品国色综合久久| 欧美国产日本韩| 欧美精品一区二区三区蜜臀| 欧美高清不卡| 亚洲夫妻自拍| 欧美1区2区| 亚洲福利专区| 亚洲精品视频一区| 一区二区三区产品免费精品久久75| 久久成人一区| 美女性感视频久久久| 久久综合中文色婷婷| 美女精品在线观看| 欧美成人亚洲成人| 欧美一区二区三区久久精品茉莉花| 久久精品国产成人| 美女亚洲精品| 亚洲已满18点击进入久久| 欧美在线观看视频一区二区三区| 久久综合伊人77777麻豆| 欧美激情 亚洲a∨综合| 欧美三级视频在线观看| 亚洲电影观看| 午夜精品免费| 理论片一区二区在线| 亚洲精选视频在线| 欧美影院在线播放| 正在播放亚洲| 国产伦精品一区二区三区免费迷| 国产精品综合| 一二三四社区欧美黄| 午夜视频久久久| 久久精视频免费在线久久完整在线看| 亚洲精品国产视频| 午夜免费久久久久| 欧美日本中文字幕| 国外成人在线| 亚洲国产片色| 久久深夜福利| 99这里只有精品| 欧美不卡高清| 国产欧美日韩不卡| 亚洲二区在线视频| 久久久999国产| 亚洲伦理在线观看| 欧美精品久久99久久在免费线| 国产日韩成人精品| 怡红院精品视频| 久久久久久久综合| 亚洲网站在线看| 欧美亚男人的天堂| 亚洲国产日韩欧美在线动漫| 久久偷看各类wc女厕嘘嘘偷窃| 一区二区三区欧美成人| 欧美成人午夜激情在线| 亚洲高清在线视频| 久久免费视频网站| 久久久99久久精品女同性| 欧美精品一区在线播放| 在线观看日韩www视频免费| 欧美在线视频免费观看| 一本色道88久久加勒比精品| 欧美二区在线播放| 最新日韩精品| 亚洲第一综合天堂另类专| 久久精品国产在热久久| 国产在线精品二区| 久久精品电影| 久久天天躁狠狠躁夜夜av| 国内精品免费午夜毛片| 欧美大片一区二区| 久久这里有精品视频| 国产精品成av人在线视午夜片| 亚洲一区免费看| 亚洲一二三级电影| 尤物九九久久国产精品的特点| 久久乐国产精品| 久久免费视频一区| 夜夜嗨av一区二区三区四区 | 亚洲国内在线| 欧美激情成人在线视频| 一区二区三区www| 亚洲免费电影在线观看| 欧美三级乱人伦电影| 麻豆精品在线视频| 欧美日韩成人在线观看| 99国产精品久久久久久久成人热| 亚洲人成在线免费观看| 欧美视频在线免费| 久久久久国产精品厨房| 久久大逼视频| 亚洲国产日韩一区| 亚洲综合激情| 亚洲第一页在线| 一本久久a久久免费精品不卡| 国产精品久久久久久久午夜| 在线电影欧美日韩一区二区私密| 亚洲美女精品久久| 国产免费亚洲高清| 亚洲观看高清完整版在线观看| 欧美日本韩国| 99国产精品久久久久老师| 亚洲视频中文字幕| 永久91嫩草亚洲精品人人| 亚洲视频大全| 亚洲日本成人| 巨乳诱惑日韩免费av| 午夜精品一区二区三区在线视 | 毛片av中文字幕一区二区| 99这里只有精品| 久久深夜福利| 亚洲视频网在线直播| 毛片一区二区| 久久久午夜电影| 久久久久久一区二区| 欧美亚洲三级| 欧美啪啪一区| 亚洲国产一区在线| 黄色一区二区在线| 欧美在线国产| 亚洲男人第一av网站| 欧美一区二区视频在线观看| 亚洲欧美精品suv| 欧美大片在线影院| 亚洲欧洲日产国产网站| 狠狠久久综合婷婷不卡| 午夜日韩激情| 亚洲日本免费电影| 欧美岛国激情| 农夫在线精品视频免费观看| 在线看片日韩| 久久久91精品| 欧美国产亚洲精品久久久8v| 欧美另类在线观看| 99xxxx成人网| 亚洲第一在线视频| 欧美成人午夜激情在线| 欧美尤物巨大精品爽| 国产日韩视频| 亚洲精品亚洲人成人网| 一区二区激情视频| 欧美另类99xxxxx| 亚洲最新在线| 亚洲免费在线看| 黄色日韩网站视频| 欧美专区日韩视频| 蘑菇福利视频一区播放| 韩国一区二区三区在线观看| 久久久久久久高潮| av成人手机在线| 午夜欧美精品久久久久久久| 欧美极品色图| 麻豆精品视频在线观看视频| 亚洲国产精品久久人人爱蜜臀| 久久久久久9| 最新国产成人av网站网址麻豆| 亚洲精品国产视频| 国产精品成人播放| 亚洲女人天堂av| 欧美国产视频在线| 亚洲婷婷综合久久一本伊一区| 国产日韩欧美成人| 久久久久久久一区二区三区| 久久久美女艺术照精彩视频福利播放 | 久久av资源网站| 国外成人免费视频| 欧美激情在线免费观看| 亚洲黄一区二区三区| 一区二区三区不卡视频在线观看 | 久久一二三国产| 亚洲国产精品一区二区www| 国产精品户外野外| 亚洲国产欧美一区二区三区同亚洲| 午夜精品亚洲| 亚洲日产国产精品| 香蕉亚洲视频| 亚洲精品中文字幕在线观看| 欧美三级乱码| 欧美成人伊人久久综合网| 99精品福利视频| 久久久久综合网| 99精品欧美一区二区蜜桃免费| 国产精品一区二区女厕厕| 另类成人小视频在线| 亚洲自啪免费| 亚洲高清资源综合久久精品|