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

隨筆 - 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>
            久久久久久夜| 中日韩男男gay无套| 欧美va天堂| 美女国产一区| 能在线观看的日韩av| 欧美岛国激情| 欧美视频导航| 国产欧美日本一区二区三区| 国产亚洲毛片在线| 亚洲人成在线影院| 亚洲伊人一本大道中文字幕| 欧美专区第一页| 免费欧美在线| 日韩一级在线| 久久精品国产999大香线蕉| 久久综合久久综合九色| 欧美日本三区| 国产拍揄自揄精品视频麻豆| 亚洲国产成人久久综合| 午夜精品视频在线观看一区二区 | 亚洲天堂成人在线观看| 午夜精品久久久久久久久久久久久 | 亚洲在线视频网站| 久久久国产91| 亚洲美女中文字幕| 欧美在线短视频| 欧美激情精品久久久| 国产女主播一区二区三区| 亚洲高清资源| 久久成人免费网| 亚洲精品人人| 久久夜色精品国产亚洲aⅴ| 欧美体内she精视频| 1000部精品久久久久久久久 | 欧美激情精品久久久久久免费印度| 亚洲精品美女在线观看播放| 亚洲视屏在线播放| 欧美精品国产一区| 亚洲福利精品| 久久久久久久精| 亚洲午夜伦理| 欧美人交a欧美精品| 1769国内精品视频在线播放| 欧美在线观看一区| 亚洲性视频h| 欧美日韩免费在线视频| 欧美成人精品1314www| 亚洲日韩欧美视频| 亚洲一区欧美二区| 欧美日韩日韩| 亚洲精品专区| 欧美丰满高潮xxxx喷水动漫| 欧美一区激情| 国外成人网址| 久久女同精品一区二区| 亚洲自拍偷拍视频| 欧美午夜宅男影院在线观看| 妖精视频成人观看www| 亚洲国产精品成人久久综合一区| 久久久精品动漫| 激情欧美日韩| 欧美成va人片在线观看| 久久久久久一区二区| 在线国产精品播放| 欧美高清视频在线| 欧美福利小视频| aa成人免费视频| 一本色道**综合亚洲精品蜜桃冫 | 美女日韩欧美| 亚洲狠狠婷婷| 亚洲免费成人av| 欧美午夜剧场| 欧美一区二区大片| 久久精品一区| 亚洲美女少妇无套啪啪呻吟| 日韩视频在线一区| 国产农村妇女精品一区二区| 久久久久国产一区二区| 久久久久国产精品一区二区| 亚洲国产婷婷香蕉久久久久久99| 亚洲国产导航| 国产精品国产福利国产秒拍| 久久精品99国产精品日本| 久久精品视频在线| 99精品国产高清一区二区 | 欧美成人a视频| 99综合在线| 亚洲欧洲av一区二区| 在线高清一区| 亚洲一区二区在线看| 国产资源精品在线观看| 欧美激情网站在线观看| 欧美三级午夜理伦三级中视频| 欧美一区二区视频观看视频| 久久全国免费视频| 中文网丁香综合网| 久久精品欧美| 亚洲宅男天堂在线观看无病毒| 欧美一区二区三区精品| 亚洲精品一区二区三区不| 午夜精品久久| 99热精品在线| 久久全国免费视频| 午夜久久一区| 欧美精品123区| 老色鬼久久亚洲一区二区| 欧美午夜精品久久久久久久| 亚洲高清视频在线| 久久国产精品网站| 欧美激情久久久| 久久香蕉国产线看观看av| 欧美三级中文字幕在线观看| 麻豆精品视频在线| 国产精品亚洲综合| 亚洲福利一区| 在线观看的日韩av| 亚洲欧美影音先锋| 一区二区免费在线观看| 久久美女性网| 久久精品夜夜夜夜久久| 欧美午夜宅男影院在线观看| 91久久精品www人人做人人爽| 一区二区三区在线看| 欧美在线资源| 久久久久久久999精品视频| 国产精品永久免费| 99精品免费视频| 中文欧美在线视频| 欧美色欧美亚洲另类七区| 亚洲人成网在线播放| 最新日韩中文字幕| 免费成人在线观看视频| 美女主播视频一区| 黑人操亚洲美女惩罚| 性欧美videos另类喷潮| 欧美亚洲色图校园春色| 国产乱码精品一区二区三区五月婷| 一本色道久久综合亚洲精品高清| 一区二区高清在线观看| 欧美日韩精品福利| 一区二区日韩精品| 亚洲视频一区二区| 国产精品久久久久毛片大屁完整版| 99国产精品一区| 亚洲与欧洲av电影| 国产精品综合不卡av| 亚洲欧美日韩一区在线观看| 亚欧成人在线| 激情一区二区三区| 免费黄网站欧美| 亚洲精品一区二区网址| 亚洲欧美日本精品| 韩国福利一区| 美国十次了思思久久精品导航| 亚洲成人直播| 一区二区三区欧美激情| 国产精品久久久久久久久免费| 亚洲一区一卡| 欧美 日韩 国产精品免费观看| 亚洲老板91色精品久久| 国产精品都在这里| 欧美一区免费| 亚洲黄色毛片| 午夜精品亚洲一区二区三区嫩草| 国产一区二区黄| 欧美福利一区二区| 午夜激情综合网| 亚洲国产欧美另类丝袜| 先锋影音久久久| 亚洲精品国产精品乱码不99| 国产精品久久久久久亚洲调教| 久久国产主播精品| 日韩视频―中文字幕| 久久不见久久见免费视频1| 亚洲第一精品夜夜躁人人爽| 欧美日韩久久精品| 久久精品欧美日韩精品| 亚洲欧美变态国产另类| 免费h精品视频在线播放| 在线亚洲观看| 欧美黄色影院| 欧美一区二区播放| 日韩亚洲在线观看| 国产一区欧美日韩| 欧美日韩免费观看一区| 久久久青草青青国产亚洲免观| av成人毛片| 亚洲高清不卡一区| 久久久欧美精品sm网站| 亚洲午夜激情| 亚洲精品乱码久久久久久久久| 国产精品日韩精品欧美精品| 老司机精品福利视频| 欧美一区二区三区在线视频| 日韩午夜激情av| 亚洲国产日韩欧美在线图片| 久久se精品一区二区| 羞羞色国产精品| 亚洲一区黄色| 国产精品99久久99久久久二8|