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

隨筆 - 10  文章 - 0  trackbacks - 0
<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

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

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

搜索

  •  

積分與排名

  • 積分 - 3919
  • 排名 - 1712

最新評論

閱讀排行榜

評論排行榜

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) 評論(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>
            99re8这里有精品热视频免费 | 免费人成精品欧美精品| 欧美国产先锋| 久久亚洲综合网| 国产精品免费网站| 亚洲精品日韩久久| 亚洲国产精品悠悠久久琪琪| 亚洲综合大片69999| 中文有码久久| 欧美经典一区二区三区| 欧美成人精品在线观看| 国产亚洲毛片在线| 亚洲欧洲av一区二区| 亚洲一区区二区| 欧美美女喷水视频| 亚洲激情在线观看视频免费| 亚洲激情视频在线| 亚洲丁香婷深爱综合| 国产精品久久久久久久免费软件| 免费日韩视频| 在线观看日韩国产| 久久成人精品| 久久久久久久久久久久久9999 | 国产精品一区一区三区| 99伊人成综合| 亚洲欧美日韩视频一区| 国产精品jizz在线观看美国 | 欧美精品九九99久久| 亚洲国产精品一区| 99re在线精品| 欧美午夜三级| 亚洲在线观看视频网站| 午夜在线a亚洲v天堂网2018| 国产精品视频一二三| 亚洲在线观看视频网站| 久久国产精品久久久| 国产一区二区三区观看 | 欧美日韩亚洲国产精品| 在线视频精品一区| 欧美一区二区精美| 韩国精品在线观看| 免费看成人av| 在线视频精品| 久久尤物视频| 亚洲精品日产精品乱码不卡| 欧美理论视频| 亚洲欧美成人综合| 美女被久久久| 一本色道久久综合亚洲91| 欧美性视频网站| 欧美在线免费视频| 欧美激情在线播放| 亚洲男人的天堂在线aⅴ视频| 国产精品美女久久久| 久久国内精品自在自线400部| 欧美chengren| 在线亚洲自拍| 黄色成人在线观看| 欧美另类一区| 欧美在线一二三区| 亚洲区一区二区三区| 欧美影院成人| 亚洲日本va午夜在线影院| 国产精品久久久久91| 久久精品人人做人人爽电影蜜月| 亚洲激情亚洲| 久久免费少妇高潮久久精品99| 亚洲精品综合在线| 国产亚洲网站| 欧美色图麻豆| 乱中年女人伦av一区二区| 在线亚洲一区| 亚洲国产精品免费| 久久久高清一区二区三区| 一本色道**综合亚洲精品蜜桃冫 | 精品69视频一区二区三区| 欧美精品在线一区| 久久精品理论片| 亚洲一区观看| 日韩视频在线观看国产| 免费h精品视频在线播放| 先锋a资源在线看亚洲| 亚洲麻豆av| 亚洲国产欧美一区| 国外成人在线视频| 国产精品一区二区在线| 欧美日韩大陆在线| 欧美freesex交免费视频| 久久久久国产精品一区| 亚洲欧美日韩中文播放| 亚洲美女在线视频| 亚洲国产色一区| 欧美大片国产精品| 欧美二区在线| 欧美插天视频在线播放| 久久人人爽爽爽人久久久| 欧美在线视频观看免费网站| 亚洲性视频h| 亚洲一区自拍| 亚洲一级黄色片| 亚洲一二三区在线| 制服诱惑一区二区| 亚洲一区二区三区中文字幕在线| 亚洲免费久久| 亚洲最新合集| 国产精品99久久久久久白浆小说| 亚洲日本va在线观看| 91久久精品国产91久久性色| 亚洲国产91色在线| 在线欧美小视频| 亚洲国产视频一区二区| 亚洲人体一区| 99精品欧美一区| 亚洲网站视频| 亚洲欧美日韩中文在线制服| 亚洲欧美高清| 久久全球大尺度高清视频| 久久综合九九| 欧美激情第8页| 亚洲三级影院| 亚洲午夜精品一区二区| 亚洲欧美中文在线视频| 久久大逼视频| 欧美 日韩 国产 一区| 欧美日韩国产另类不卡| 国产精品日韩在线一区| 国产一区二区三区久久| 亚洲激情成人| 亚洲视频一起| 久久蜜臀精品av| 亚洲国产天堂久久综合网| 夜夜嗨av一区二区三区| 欧美亚洲一区| 欧美成人蜜桃| 国产精品私拍pans大尺度在线| 国内外成人免费激情在线视频网站| 一区二区三区在线看| 99精品国产一区二区青青牛奶| 亚洲欧美日本伦理| 久久另类ts人妖一区二区| 亚洲第一页在线| 亚洲一二三区在线| 浪潮色综合久久天堂| 欧美午夜片在线观看| 国产一区观看| av成人动漫| 久久视频在线免费观看| 亚洲精品日韩精品| 久久国产精彩视频| 欧美日韩一区二区三区在线观看免 | 欧美区一区二| 国产一区二区三区在线播放免费观看| 亚洲国产片色| 欧美一区精品| 亚洲每日在线| 久久久亚洲午夜电影| 国产精品久久久久久久久久久久| 一色屋精品视频在线观看网站| 亚洲免费成人av| 久久久精品一区二区三区| 亚洲精品网址在线观看| 久久久久国产精品www | 欧美日韩精品在线观看| 红桃视频一区| 午夜精品视频网站| 亚洲人成人一区二区三区| 久久精品视频免费| 国产精品中文在线| 一区二区三区毛片| 亚洲国产日韩美| 久久色中文字幕| 韩国一区二区三区美女美女秀| 在线综合亚洲| 亚洲三级影院| 欧美刺激午夜性久久久久久久| 黑人巨大精品欧美一区二区| 亚洲欧美福利一区二区| 日韩亚洲欧美中文三级| 麻豆乱码国产一区二区三区| 国内伊人久久久久久网站视频| 亚洲欧美在线aaa| 一区二区三区欧美成人| 欧美精品一卡二卡| 亚洲精品一区二区三区樱花| 嫩草影视亚洲| 麻豆精品91| 亚洲第一伊人| 欧美大片国产精品| 免费成人av在线看| 91久久中文| 亚洲破处大片| 欧美日韩精品是欧美日韩精品| 亚洲精品久久视频| 亚洲大胆人体视频| 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久视频在线免费观看| 久久精品青青大伊人av| 在线播放一区| 亚洲高清在线观看| 欧美片在线观看|