Re: lvalues and rvalues



On Apr 7, 1:58 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Nicklas Karlsson <karlsson.nick...@xxxxxxxxx> writes:
Let me summarize what I make of this (hopefully its correct):

What is an lvalue?
An expression that designates (identifies) an object, when evaluated
the compiler knows what object is being designated and what type that
object has.

Close.  Evaluation happens at run time; the compiler doesn't
necessarily know which object is being designated.  It does know
the type of the object -- or rather the type imposed by the lvalue.
This might not be the declared type of the object itself.  In fact,
the object might not even have a declared type.

Some examples (I haven't taken the time to test these):

    int x, y, *ptr;
    switch (rand() % 3) {
        case 0: ptr = NULL; break;
        case 1: ptr = &x;   break;
        case 2: ptr = &y;   break;
    }
    *ptr; /* an lvalue, but the compiler has no idea whether it
             designates x, y, or no object at all */

    void *vptr = malloc(sizeof(int));
    assert(vptr != NULL);
    /* We've just created an object, but it has no inherent type. */
    *(int*)vptr;      /* the type is imposed by the lvalue */
    *(unsigned*)vptr; /* same object, different lvalue, different type */

What is an rvalue?
The result of an expression that is not an lvalue appearing in LHS (an
lvalue, when evaluated, yields an rvalue if it appears on RHS, that
is, the value of the object being designated is being fetched, however
if its on LHS it does not evaluate to an rvalue but rather evaluates
so the compiler knows where to store an rvalue, that is, it identifies
an object and what type that object has).

Too many words.  An rvalue is the value of an expression, that's all.
It's also a term that the Standard doesn't use, and I see little point
in using it while discussing C.  <OT>(C++ is another matter.)</OT>

What is an object?
A region of data storage, usally an object is a place in memory, so it
usally has a starting address, but it could also be stored in other
ways, for example in the CPU's register and therefor won't have an
ordinary address.

I'd say "address" rather than "starting address".  In C, an address is
typed.  It doesn't point to the beginning of an object, it points to
the object.

Silly question, the lvalue (expression) itself has no type right? But
when its evaluated the compiler finds out what type the object that
the lvalue identifies has?

No, an lvalue, like any expression, has a type.


At least 2 good things happened in this thread. OP surmised what he
had gathered. You included referent source to illustrate. Makes for
good reading.
--
.



Relevant Pages

  • Re: object system...
    ... So a C compiler that does that sort of optimization is not a valid C compiler because the language semantics are being violated. ... Note that even if one writes a very smart debugger that looks at the register, the language semantics is still being broken because the lvalue memory location is not being updated as the instructions are executed. ... optimizing an lvalue into a register violates the spec in a manner that is usually not a problem so that optimization is deemed justified. ...
    (comp.object)
  • Re: A non-const reference may only be bound to an lvalue?
    ... lvalue_cast(const T& rvalue) ... non-const reference (lvalue), I think there may be still risks. ... originally, rvalue const reference may be binded to some constant, and if you ... it just tricks the compiler into ...
    (microsoft.public.vc.language)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... An 'lvalue' is *not* any kind of value, ... function it allocates more memory and copies the literal to the new ... might keep a copy of the pointer, and store that pointer other ... and the C compiler will already forbid you ...
    (comp.lang.c)
  • Re: Rvalue of struct type
    ... "Lvalue required". ... array type returned by a function is converted from Rvalue to Lvalue! ... and *.a + 0) is a dereferenced pointer. ... (The compiler I'm trying right now ...
    (comp.lang.c)
  • Re: Rvalue of struct type
    ... "Lvalue required". ... array type returned by a function is converted from Rvalue to Lvalue! ... struct S s; ... (The compiler I'm trying right now ...
    (comp.lang.c)