0

I assume when we declare int a ; a=10;, then the compiler takes r-value & put it in it's l-value. At that time the l-value always treated as a address in the memory location i.e. a or any other variable in assignment operation at the left hand side treated as (&variable). So we are able to catch its address to put the r-value in it.

Is my assumption is right or wrong?If wrong, then what is basic idea behind it?

Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99
SumS
  • 25
  • 6
  • "Container class" is a C++-specific term. – Keith Thompson Jul 17 '14 at 16:06
  • I believe you are *effectively* correct, remember the compiler converts that into native machine code I would guess it's going to be a [`MOV`](https://en.wikipedia.org/wiki/MOV_%28x86_instruction%29) instruction. It depends on what you mean by "container class", I assume you mean type. – Elliott Frisch Jul 17 '14 at 16:08

3 Answers3

2

You're making it more complicated than it needs to be. The language definition simply states:

An assignment operator stores a value in the object designated by the left operand.

(6.5.16, para 3).

The only general constraint is that the left operand be a modifiable lvalue. An lvalue can correspond to a register (which has no address) or an addressable memory location. Exactly how it happens is an implementation detail.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 1
    Or, the lvalue might have no physical representation in the final program at all: the compiler might be able to short-cut all uses of that variable and assign the value directly to it's final destination. – ams Jul 17 '14 at 16:49
1

An assignment operator has two operands. The left operand must be a "modifiable lvalue".

An "lvalue" is, roughly, an expression that designates an object.

(An "rvalue", as the C standard uses the term, is simply the value that results from evaluating an expression. The C standard defines the term, but rarely uses it. The usage is a bit inconsistent: an "lvalue" is an expression, but an "rvalue" is a value.)

The right operand is evaluated to yield a value. That value is then converted, if necessary, to the type of the left operand, and the result is stored in the object designated by the left operand.

Addresses are not required. For example, a variable defined with register has no address, but it can still be the target of an assignment.

If you want the gory details, see section 6.5.16 of the C standard (the link is to n1570.pdf, the most recent draft, a 1.7-megabyte PDF).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • in simple words the modifiable l-value is get replaced by new r-value is it ? – SumS Jul 17 '14 at 16:25
  • @SumeetBajiraoGaikwad: Not really. An *lvalue* is not an object; it's an expression that designates an object. An *rvalue* (a term the C standard defines but rarely uses) is just the value of an expression. The value that results from evaluating the RHS is stored in the object designated by the LHS, replacing whatever value (if any) it previously had. – Keith Thompson Jul 17 '14 at 16:29
  • @SumeetBajiraoGaikwad - No. The value *in* the lvalue is replaced. – Hot Licks Jul 17 '14 at 16:29
  • @HotLicks: No. An lvalue is a kind of expression. There is no "value in the lvalue". There is a value *of* the object *designated by* the lvalue. – Keith Thompson Jul 17 '14 at 16:32
  • @KeithThompson - Yeah, you can dance around the terminology a lot. The more precisely you try to define it, the harder it gets to understand. (But, yes, "designated" is probably the better term.) – Hot Licks Jul 17 '14 at 16:36
  • @HotLicks: I disagree. These words have very specific meanings, and using them imprecisely IMHO is what leads to confusion. – Keith Thompson Jul 17 '14 at 17:29
  • @KeithThompson - The point is that the "very specific meanings" are understood by very few people, and to most of the rest they appear to be gibberish. – Hot Licks Jul 17 '14 at 17:48
  • @HotLicks: Which is why I explained their meanings. I suggest using words like "lvalue" and "rvalue" either correctly or not at all. – Keith Thompson Jul 17 '14 at 17:52
  • Where did you explain their meanings?? – Hot Licks Jul 17 '14 at 17:53
  • @HotLicks: In my answer. (I added an explanation of "rvalue" just a moment ago, copied from a comment I posted about an hour ago.) – Keith Thompson Jul 17 '14 at 17:54
  • If you say so. I don't see it. – Hot Licks Jul 17 '14 at 17:55
  • @HotLicks: Second and third paragraphs. What are you missing? – Keith Thompson Jul 17 '14 at 17:56
  • "An expression that designates an object" is meaningless to anyone but perhaps a C standards lawyer. But I'm done here. – Hot Licks Jul 17 '14 at 17:57
  • @HotLicks: If you don't understand it, feel free to ask for clarification. If you think others won't understand it, *they* should feel free to ask for clarification. I welcome *constructive* criticism. – Keith Thompson Jul 17 '14 at 17:59
0

The terms "lvalue" and "rvalue" come from being on the left and right of the = symbol and hence are self-reflexive definitions. (Ie, not real helpful.)

But the important point is that an rvalue must be able to produce a value (ie, finite pattern of bits), while an lvalue must be capable of being having assigned to it a similar value.

As such, an lvalue must ultimately represent a storage address or register or I/O port or something else (is there anything else?) that can be "written to". But this does not mean that the lvalue contains an address -- it merely represents the addressable entity in the compiler, and will be translated to "real" addressing info in the generated assembly. Ie, it's simply a name for something.

An rvalue, on the other hand, need not represent a "real thing", but can be a literal or some expression or call or whatever, so long as it produces a "value" when asked.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151