Left and right

Source: Internet
Author: User

Lvalueness is an important attribute of expressions in C/C ++. Only one left-value expression can be used.
To reference and change the value of an object. (In some cases, the right expression can also be referenced (refer) to
Objects, and the value of the object may be indirectly modified, as described later ).
 
What is an object? If it is not explicitly stated, the objects mentioned here are compared with the narrow class/object,
More extensive. In C/C ++, the so-called object refers to a region of storage in the execution environment ),
The content in the storage area indicates the value of the object ). Notice what we call "Generation
Table ", for an object, if we need to extract (FETCH) its value, we need to reference it through a certain type. Using different types, interpreting the content of the same object may lead to different values or some undefined behaviors.
Before introducing the left value, we also need to introduce a concept: variable ). Variables and
Like the two. What is a variable? The so-called variable is a declaration. Through the Declaration, we put a name and
When we use this name, it indicates that we perform some operation on this object. But not
Is that each object has a name, and does not mean that there is a corresponding variable. For example, a temporary object (temporary object)
There is no association between names (do not call it a temporary variable by mistake, which is incorrect ).
 
1. Left value in C

1.1
According to the definition of C, the left value is an expression referenced to an object. With the left value, we can retrieve the value of this object. Connect
You can also modify the value of this object through the modifiable lvalue expression. (It must be noted that,
In C ++, the left value can also be referenced to a function, that is, if expression F references a function type, it is neither
The left value is not the right value, but the left value in C ++ ). Because the left value references an object, we use
When the value expression (or only the left value expression and function) is used to retrieve the address of the object, the address of the object can be obtained (there are two types of left
The value expression cannot take the address. First, it has a bit-field type, because the minimum addressing unit in the implementation is byte;
The other is the variable compiler with the register identifier, which may be optimized to the register using the register modifier ).

Ex1.1
Char A [10]; // A is an lvalue representing an array of 10 ints.
Char (* P) [10] = & A; // & A is the address of the array.
Const char * P = "Hello World"; // "Hello world" is an lvalue of type char [12]
// In C, type const char [12] in C ++.
Char (* P) [12] = & "Hello World ";

Struct s {int A: 2; int B: 8 ;};
Struct s t;
Int * P = & T. A; // error. t. A is an lvalue of bitfield.

Register int I;
Int * P = & I; // error. I is an lvalue of register type.
Int A, B;
Int * P = & (a + B); // error. A + B is not an lvalue.

1.2
Assume that expr1 is an object type or an incomplete type (incomplete type, that is, the layout of this type and
Small unknown) pointer, then we can assert that * expr1 must be a left value expression, because according to the * operator definition,
* Expr1 indicates the object to which expr1 is referenced. If expr1 is a simple name, it represents a variable.
Similarly, this expression is also a left value because it represents the object corresponding to the variable. For subscript operators, I
We can make the same conclusion, because expr1 [expr2] is always equal to * (expr1) + expr2), then
P-> member is also a left-value expression. However, for expr1.expr2, we cannot determine that it is a left value.
Expression. Because expr1 may not be the left value.
It should be particularly noted that the Left value is only the static attribute of the expression. When we say that an expression is the left value,
It does not mean that it must reference a valid object. Int * P; * P is the expression of the Left value.
* The read and write results of the object referenced by P may be undefined.
Ex1.2
Extern struct a;
Struct a * P2 = &;
 
A is an expression of the Left value, so it can perform the & operation. However, stru A is still incomplete.

// In C ++
Extern Class;
A & R = A; // OK. refers to A, though a with an incomplete type.

1.3 modifiable left Value
In terms of semantics, in the expression of the object corresponding to the left value, the left value must be a modifiable left value. For example
The left operand in the value (including the compound value assignment) expression must be a modifiable left value expression; Auto-increment/Subtraction
Operator.
Ex1.3
Const int A [2], I; // Note: A unintialized. Legal in C, illegal in C ++.
I ++; // error, I is an lvalue of Type const Int.
A [0] --; // error, a [0] is an lvalue of const Int.

1.4 Right Value
Another concept that corresponds to the left value is the right value (Rvalue ). In C, the right value also uses the value of the expression (value
The expression. That is, the right value emphasizes not the expression itself, but the result after the expression operation. This
Results often do not reference a certain object and can be regarded as intermediate results of calculation. Of course, it may also reference a certain pair
Object, but we cannot directly modify the object through the right expression.

1.4.1 storage location of the right value
Ex1.4
Int I;
I = 10;
10 is an expression of the right value. the semantics of the preceding sentence is to modify the object referenced by I with the value of integer constant 10.
From the Assembly Language Perspective, the above statements may be translated:
MoV addr_of_ I, 10;
10. The value is hardcoded into the machine command;

The right value can also be stored in the register:
Int I, J, K;
K = I + J;
The I + J expression is a right value, which may be stored in registers.
MoV eax, dword ptr [addr_of_ I];
MoV EBX, dword ptr [addr_of_j];
Add eax, EBX;
MoV dword ptr [addr_of_k], eax;
Here, the result of the I + J expression is in eax, and the result is the value of the I + J expression, which does not reference a certain object.

In some cases, a right-value expression may also reference an object.
Struct s {char C [2];};
Struct s f (void );

Void g ()
{
F (). I;
F (). c [1]; // (*)
}
The F () expression is a function call. The expression type is the return type struct s of F, and the F () expression is the right value table.
It usually corresponds to an object, because the return value of the function is a structure.
An object (a storage area) is hardly competent to use registers, and the [] operator must be referenced in semantics.
To the object.
Although the right value may be referenced to an object, it must be noted that in the right value expression, whether to reference the object and reference
The lifetime of the obtained object is often not controlled by the programmer.

1.4.2
Why do we need the right value? The right value indicates the value after an expression operation. This value is not specified in the bucket.
When we need a value after expression operation, that is, we need the right value. For example, in the Value assignment operation, A = B; we need to use the table
To modify the object represented by. If expression B is a left-value expression, we must start with the object represented by expression B.
And then use this value to modify the object represented by. This removal process is actually
Conversion from left to right
Article Source: http://www.diybl.com/course/3_program/c++/cppjs/200827/99695.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.