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

隨筆 - 10  文章 - 0  trackbacks - 0
<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

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

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

搜索

  •  

積分與排名

  • 積分 - 3838
  • 排名 - 1718

最新評論

閱讀排行榜

評論排行榜

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 閱讀(540) 評論(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>
            亚洲美女精品成人在线视频| 久久在线播放| 欧美午夜久久久| 欧美日本免费一区二区三区| 欧美精品一区二区久久婷婷| 久热精品视频在线免费观看| 久久综合久色欧美综合狠狠| 麻豆九一精品爱看视频在线观看免费 | 亚洲一区二区视频在线观看| 亚洲一级片在线观看| 亚洲免费伊人电影在线观看av| 午夜激情亚洲| 欧美成人精品一区二区三区| 国产精品成人免费视频| 韩国av一区二区三区在线观看| 在线免费日韩片| 一区二区激情| 久久女同互慰一区二区三区| 亚洲激情欧美| 亚洲一区二区毛片| 久久人人爽国产| 欧美色123| 亚洲电影免费观看高清| 亚洲一区免费| 欧美大片在线看| 亚洲一区视频| 欧美精品一区三区| 国产一区二区三区的电影| 亚洲精品在线观看视频| 久久aⅴ国产欧美74aaa| 欧美日韩国产美| 国产欧美精品va在线观看| 亚洲国产精品久久久久婷婷老年| a4yy欧美一区二区三区| 久久超碰97中文字幕| 亚洲国产美国国产综合一区二区| 亚洲制服欧美中文字幕中文字幕| 免费亚洲网站| 国产一区二区三区四区老人| 亚洲视频在线一区观看| 亚洲第一色在线| 久久高清免费观看| 国产精品久久久久久久app| 亚洲激情在线观看| 久久久夜夜夜| 欧美亚洲免费高清在线观看| 欧美性大战xxxxx久久久| 99热这里只有精品8| 欧美高清视频在线| 久久嫩草精品久久久精品一| 国产一区二区精品久久99| 午夜一区二区三区在线观看| 亚洲免费观看在线观看| 欧美久久久久久| 夜夜嗨一区二区三区| 欧美激情精品久久久久久久变态 | 亚洲高清毛片| 巨胸喷奶水www久久久免费动漫| 国产一区亚洲一区| 久久婷婷丁香| 久久精品国产99国产精品| 国产午夜精品理论片a级探花| 午夜精品福利视频| 亚洲欧美日韩电影| 国产日韩亚洲欧美精品| 久久久www成人免费精品| 欧美在线视频免费| 激情久久久久久久| 欧美成熟视频| 欧美日韩一区二区在线| 亚洲无线一线二线三线区别av| 亚洲精品美女在线| 欧美性大战久久久久久久蜜臀| 亚洲欧美在线免费| 午夜久久电影网| 在线欧美日韩精品| 亚洲精品久久在线| 国产精品亚洲аv天堂网| 久久精品国产亚洲aⅴ| 久久男人资源视频| 国产精品99久久久久久久久久久久| 亚洲深夜福利| 黄色成人av网站| 亚洲精品极品| 国产日韩欧美制服另类| 国产精品高清网站| 国产精品成人一区二区网站软件| 亚洲一区二区三区在线看| 亚洲一区二区三区精品在线| 国产一区二区三区免费在线观看| 麻豆亚洲精品| 欧美日韩视频在线第一区| 校园激情久久| 欧美sm视频| 久久大逼视频| 欧美国产日韩在线观看| 欧美一进一出视频| 免费日韩成人| 香蕉精品999视频一区二区| 另类激情亚洲| 午夜日韩在线| 欧美激情视频给我| 欧美在线电影| 欧美日韩国产免费| 美女主播一区| 国产精品婷婷午夜在线观看| 欧美激情第9页| 国产啪精品视频| 亚洲精品影院| 亚洲国产精品成人一区二区| 亚洲欧美日韩一区二区在线| 日韩一区二区电影网| 久久精品亚洲精品| 欧美亚洲免费| 欧美亚洲成人网| 亚洲日本中文字幕免费在线不卡| 国产午夜精品理论片a级大结局| 一本色道久久加勒比精品| 亚洲欧洲精品一区二区三区| 久久成人一区| 久久国产免费看| 欧美午夜www高清视频| 免费成人网www| 国产精品sm| 夜夜嗨av一区二区三区中文字幕| 在线观看精品一区| 久久精品99国产精品日本| 亚洲伊人第一页| 欧美日韩国产综合在线| 欧美成熟视频| 在线不卡中文字幕播放| 性做久久久久久免费观看欧美 | 亚洲一区在线观看视频 | 激情久久影院| 在线视频亚洲一区| 亚洲美女网站| 欧美日韩黄色大片| 亚洲激情另类| 欧美一区成人| 亚洲一区三区视频在线观看| 欧美黄色日本| 欧美激情四色| 亚洲第一区在线| 免费欧美高清视频| 亚洲国产三级| 夜夜嗨网站十八久久| 欧美三级视频| 香蕉久久夜色精品国产| 亚洲专区在线视频| 欧美高清视频| 蜜桃久久av一区| 国产精品乱码一区二区三区| 亚洲精品一区二区三| 久久精品伊人| 日韩小视频在线观看| 久久综合99re88久久爱| 精品电影在线观看| 久久人91精品久久久久久不卡| 日韩视频免费在线| 欧美日韩不卡在线| 中文久久精品| 亚洲午夜一区二区| 国产乱码精品一区二区三区av| 亚洲天堂免费观看| 亚洲网友自拍| 国产在线一区二区三区四区| 久久亚洲精品欧美| 欧美日本高清| 老司机免费视频久久| 欧美日韩hd| 久久精品在这里| 欧美福利在线| 久久精品国产v日韩v亚洲 | 亚洲网友自拍| 亚洲国产精品v| 亚洲午夜免费视频| 亚洲欧洲三级| 欧美在线欧美在线| 99精品欧美一区二区蜜桃免费| 亚洲美女性视频| 在线欧美亚洲| 一本色道久久综合一区| 国产在线麻豆精品观看| 国产精品美女久久久久av超清 | 久久另类ts人妖一区二区| 欧美日韩国产综合视频在线观看| 欧美一级夜夜爽| 欧美精品一区二区蜜臀亚洲| 久久久国产精品一区| 国产精品久久久一本精品| 亚洲第一偷拍| 亚洲黄色影院| 久久综合给合| 欧美激情麻豆| 亚洲视频一二区| 欧美大片在线观看一区| 欧美成人第一页| 亚洲伦理久久| 欧美午夜精彩| 欧美一区亚洲一区|