Re: lvalues and rvalues
- From: frank <abqhandyman@xxxxxxxxx>
- Date: Wed, 7 Apr 2010 23:57:56 -0700 (PDT)
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.
--
.
- References:
- lvalues and rvalues
- From: Nicklas Karlsson
- Re: lvalues and rvalues
- From: Kenneth Brody
- Re: lvalues and rvalues
- From: Nicklas Karlsson
- Re: lvalues and rvalues
- From: Keith Thompson
- lvalues and rvalues
- Prev by Date: Re: Turbo C
- Next by Date: Re: C the complete nonsense
- Previous by thread: Re: lvalues and rvalues
- Next by thread: Re: lvalues and rvalues
- Index(es):
Relevant Pages
|