What exactly is lvalue & rvalue (old c.l.c. posts are all over the map)?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi,

Does anyone here have a strong understanding for the meanings of the
terms "lvalue" and "rvalue" as it pertains to C, objects, and different
contexts?  If so please share.

I've been reading several old posts/threads on the subject, and they
never end with a conclusion (people keep correcting each other and
disagreeing).

My take on it is that an "lvalue" is an expression that refers to an
object (which can have (a) value(s) within it), and "rvalue" is an
expression that only has a value (ephemeral value as Chris Torek would
claim) and no association with an object.

As far as their use, an "lvalue" that refers to an object of type T,
can be used anwhere an "rvalue" that that has a type T can be, but not
vice versa.  So if one uses an lvalue that refers to an int variable in
an context that requires an int value, then simply the value sitting in
the object is dumped into that context.

Is this a fair description?

0
Reply wwromeo (17) 2/6/2005 5:07:47 AM

See related articles to this posting


Romeo Colacitti wrote:
> Hi,
>
> Does anyone here have a strong understanding for the meanings of the
> terms "lvalue" and "rvalue" as it pertains to C, objects, and
different
> contexts?  If so please share.
>
> I've been reading several old posts/threads on the subject, and they
> never end with a conclusion (people keep correcting each other and
> disagreeing).
>
> My take on it is that an "lvalue" is an expression that refers to an
> object (which can have (a) value(s) within it), and "rvalue" is an
> expression that only has a value (ephemeral value as Chris Torek
points out) >and no association with an object.
>
> As far as their use, an "lvalue" that refers to an object of type T,
> can be used anwhere an "rvalue" that that has a type T can be, but
not
> vice versa.  So if one uses an lvalue that refers to an int variable
in
> an context that requires an int value, then simply the value sitting
in
> the object is dumped into that context.
>
> Is this a fair description?

Both "lvalue" and "(r)value" [current standards prefer to leave out the
'r' and  insist that the 'l' means 'locator'] are expressions.

Some expressions are lvalues, while others are rvalues. By "are" I
mean, "evaluate to results that are." "Expression" need not be a full
expression  but refers also to subexpressions too (even down to a
token-sequence for a variable name).

Every lvalue is converted to the corresponding (r)value represented in
it's object when used in a context that does not need an object
("value" context), EXCEPT for an lvalue referring to and array object
of type T (it is converted to an (r)value equal to the address of the
first element of the array and of type pointer to T).

When an lvalue is used in an "general object context," then the lvalue
is directly acted upon (no conversion to an rvalue takes place).
Examples are & and sizeof.

There is another special type of "object context" that requires not
only an lvalue, but a MODIFIABLE lvalue. These "special objects
contexts" include expressions involved with ++, --, and the left hand
sides of both = and op= . So only lvalues that are modifiable can be
used here, and they include all lvalues that are NOT: array names,
connected with objects declared as const, or connected with objects of
incomplete type (these are nonmodifiable lvalues).


All other contexts/operators (unless I missed some) require (r)values
as their expressions/operands.  For example, arguments in function
calls are expected to be (r)value expressions (but we also lvalues
expressions, but they are automatically converted to the (r)values
represented by their associated objects).
[Another way to say this is, the function call arguments is of "value"
context]

Hope this helps.

0
Reply LookSkywalker (71) 2/6/2005 5:48:23 AM

Romeo Colacitti wrote:
> 
> Hi,
> 
> Does anyone here have a strong understanding for the meanings of the
> terms "lvalue" and "rvalue" as it pertains to C, objects,
> and different contexts?  If so please share.
> 
> I've been reading several old posts/threads on the subject, and they
> never end with a conclusion (people keep correcting each other and
> disagreeing).
> 
> My take on it is that an "lvalue" is an expression that refers to an
> object (which can have (a) value(s) within it), and "rvalue" is an
> expression that only has a value (ephemeral value as Chris Torek would
> claim) and no association with an object.
> 
> As far as their use, an "lvalue" that refers to an object of type T,
> can be used anwhere an "rvalue" that that has a type T can be, but not
> vice versa.
> So if one uses an lvalue that refers to an int variable in
> an context that requires an int value,
> then simply the value sitting in
> the object is dumped into that context.
> 
> Is this a fair description?

The lvalue, rvalue distinction 
is something that can be determined at compile time.
If you have
    int array[1];
then
    array[-1] is an example of an lvalue which doesn't refer
to an object. The use of such an lvalue would be undefined behavior.

-- 
pete
0
Reply pfiland (6614) 2/6/2005 6:46:51 AM

In article <4205BD5C.6DC8@mindspring.com>
pete  <pfiland@mindspring.com> wrote:
>The lvalue, rvalue distinction 
>is something that can be determined at compile time.

Well, in C99, maybe. :-)  The C89 definition says, in part, that
if one has a pointer of type "T *":

    T *p;

then *p is an lvalue if and only if p actually points to an
object.  Thus, in:

    p = malloc(sizeof *p);
    *p = some_T_value();

"*p" is an lvalue if malloc() succeeded, but not if it failed
(returned NULL).

This is of course a ridiculous situation, which is why the N869
draft wording says that *p is an lvalue in all cases -- even if
p==NULL for instance -- but that the effect is undefined if p does
not point to a valid object of type T.

Unfortunately, the C99 definition is apparently defective as well
(see past discussion here and in comp.std.c).

The terms date back to (at least) Algol, and the intent is clear
enough: lvalues occur on the left side of assignment operators,
and rvalues occur on the right -- hence the names "left value" and
"right value".  In languages that lack C's profusion of operators,
a simple definition like this suffices; we write:

    a := b;

and there is nothing like "b++" to clutter up the issue.  C mixes
everything up into a wonderful, confusing jumble, and even
compiler-writers sometimes get it wrong. :-)

>If you have
>    int array[1];
>then
>    array[-1] is an example of an lvalue which doesn't refer
>to an object. The use of such an lvalue would be undefined behavior.

Again, apparently true in C99, but not (technically) in C89.  But
this just means the C89 standard has a defect.
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
0
Reply nospam252 (1722) 2/6/2005 8:19:12 AM

Luke Wu wrote:
>
> When an lvalue is used in an "general object context," then the
lvalue
> is directly acted upon (no conversion to an rvalue takes place).
> Examples are & and sizeof.
>

sizeof takes more than lvalues, consider...

sizeof(int *)
sizeof('A')
sizeof(33.029e-3LD)

so are types and constants objects too (note, size of directly taking
the objects as input above, not just lvalues that refer to the objects)


here is another example

sizeof("String Literal")

here, size is receiving only a pointer to the first element of the
string ('S'), so its equivalent to: sizeof(char *)

but that's not what we get, sizeof actually returns the size of the
whole string literal

I don't think sizeof fits cleanly with the theory of lvalues/rvalues.

0
Reply kobu.selva (53) 2/6/2005 5:04:38 PM

>Luke Wu wrote:
>> When an lvalue is used in an "general object context," then the
>>lvalue is directly acted upon (no conversion to an rvalue takes
>>place).  Examples are & and sizeof.

In article <1107709477.995487.296710@f14g2000cwb.googlegroups.com>
Kobu <kobu.selva@gmail.com> wrote:
>sizeof takes more than lvalues ...

Yes; and it also has more than one syntax:

    sizeof expr
    sizeof ( type-name )

An expression can, but need not, include outer parentheses; but
to use sizeof on a type-name you must use the parentheses.

>consider...
>
>sizeof(int *)

This one requires the parentheses.

>sizeof('A')
>sizeof(33.029e-3LD)

These two do not.  (But "e-3LD" is syntactically wrong; I assume
you mean "e-3L", to make it a long-double constant.)

>so are types and constants objects too (note, size of directly taking
>the objects as input above, not just lvalues that refer to the objects)

No, but they *are* expressions.

>here is another example
>
>sizeof("String Literal")
>
>here, size is receiving only a pointer to the first element of the
>string ('S'), so its equivalent to: sizeof(char *)
>
>but that's not what we get, sizeof actually returns the size of the
>whole string literal
>
>I don't think sizeof fits cleanly with the theory of lvalues/rvalues.

This is a more interesting case, because of an earlier comp.lang.c
discussion about string literals as initializers:

    char s1[] = "this is OK";
    char s2[] = ("but this is not");

A string literal -- which is a source code construct, rather than
something you might see at runtime -- can be used as an initializer
for an object of type "array N_opt of char", but if it is to be
used this way, it *must not* be enclosed in parentheses.  A number
of compilers allow the parentheses anyway, no doubt because their
parsers have stripped them off by the time the partially-digested
token-sequence is delivered to the part of the compiler front-end
that finishes decorating the parse tree (adjusting types, adding
conversions where implied, and so on).

All of this is something of an aside, though, because given:

    char buf[20];

we know that:

    sizeof(buf) == sizeof buf

and both arguments to the equality operator are (size_t)20.  The
implication here is that, although an array may be surrounded by
parentheses in an expression, it remains an array: it does not
undergo the "degeneration" or "decay", as some like to call it,
that converts "array N of T" to "pointer to T" merely because it
is parenthesized.  (It merely happens that some compilers do this
parentheses-stripping a bit "overzealously", as it were, so that
the string-literal-as-initializer works even when a diagnostic is
required.)

The whole point of the "object context" vs "value context" that
Luke Wu brings up is to maintain, within the compiler's parse-tree
code, the notion of whether we want to convert array-objects to
pointer-values by computing &arr[0].  (In addition, we must also
remember whether we need to fetch the value of an ordinary object,
so that in:

    int a = 3, b = 5;
    ... any (or no) code that does not change a or b ...
    a = b;

we put the value/"rvalue" of b -- 5 -- into the ["lvalue"] object
a, rather than fetching a's previous value of 3, and trying to put
b's value into 3.)  Inside the compiler, this context is generally
implicit: we know, based on the operator(s), whether we want to
find the actual *value* of "a" (3, in this case), or simply remember
the *name*:

       =
     /   \
    a     b

can be optimized to:

       =
     /   \
    a     5

(because b is still known to be 5), but not to:

       =
     /   \
    3     5

which is nonsensical.  This property of "I want a value on the
right, but an object on the left" is associated with the ordinary
assignment operator "=".

Now, there *is* a significant difference between the sizeof and
assignment operators here, in that sizeof permits any expression
of any (legal) type *as well as* an object-name, while "=" demands
*only* an object-name ("lvalue") on the left: "3 = 5;" is an
error, but "sizeof 3" is OK.

All this means is that, in the part of the compiler that deals
with an "=" operator, we have:

    /* assume "struct tree *tree" and tree->op is the op, tree->left
       is the LHS and tree->right is the RHS, with tree->monad #defined
       as either tree->left or tree->right for monadic (unary)
       operators */

    switch (tree->op) {
    ...
    case ASSIGN:
        if (!is_lvalue(tree->left))
            error("assignment operator requires an lvalue");
        tree->right = rvalue_convert(tree->right, get_typecode(tree->left));
        /* rvalue_convert produces the error if the conversion is invalid */
        break;

while in the code for "sizeof" we have:

    case SIZEOF:
        typecode = get_typecode(tree->monad);
        if (is_incomplete_type(typecode)) /* includes sizeof(void) */
            error("sizeof incomplete type");
        if (is_function_type(typecode))
            error("cannot take size of function");
        tree->type = TYPE_SIZE_T;
        tree->value = type_size(typecode) / type_size(TYPE_CHAR);
        tree->is_constant = 1;
        /* the division is to get the size in bytes rather than bits */

        tree_releasenode(tree->monad); /* no longer needed */
        break;

In other words, we do not need to *check* whether the argument to
sizeof is an object or a value, nor do we have to pass it into the
part of the compiler that extracts an rvalue from an lvalue (which
I called "rvalue_convert" here) if necessary, because all we care
about, in evaluating the "sizeof" operator, is the *type* of the
argument to sizeof.  (This is no longer true in C99, where we have
to check whether the argument is a VLA and perhaps generate code
at runtime rather than just marking the result as "is a constant".
But C99 is a much more complicated language than C89.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
0
Reply nospam252 (1722) 2/6/2005 6:47:58 PM

Chris Torek wrote:
> In article <4205BD5C.6DC8@mindspring.com>
> pete  <pfiland@mindspring.com> wrote:
> >The lvalue, rvalue distinction
> >is something that can be determined at compile time.
>
> Well, in C99, maybe. :-)  The C89 definition says, in part, that
> if one has a pointer of type "T *":

snipping

> >If you have
> >    int array[1];
> >then
> >    array[-1] is an example of an lvalue which doesn't refer
> >to an object. The use of such an lvalue would be undefined behavior.
>
> Again, apparently true in C99, but not (technically) in C89.  But
> this just means the C89 standard has a defect.
>

C99 is worse, because as per the said standard almost every expression
that doesn't resolve to an incomplete types or function type is an
LVALUE.  So something like 2+3 is an lvalue in C99.  I'll just modify
that errant sentence in the C99, it must obviously be a typo (they
forgot to mention that an lvalue "designates an object.")

Thanks for the help everyone.

0
Reply wwromeo (17) 2/6/2005 8:05:48 PM

pete wrote:
> Romeo Colacitti wrote:
> >

/

> >
> > Is this a fair description?
>
> The lvalue, rvalue distinction
> is something that can be determined at compile time.
> If you have
>     int array[1];
> then
>     array[-1] is an example of an lvalue which doesn't refer
> to an object. The use of such an lvalue would be undefined behavior.
>

Yes, "lvalues" and "objects" are not the same thing.

An lvalues are just an expression (or the resolution of an expression)
that is of a form that can normally be used to designate objects (in
your case, array[-1] is of a form that is normally used to designate an
object, but this specific case is not referring to a valid object -
thus undefined behaviour).

Objects can also exist without lvalues . For example,

1)

char *ptr = "Hello";
ptr = NULL;

/* Hereafter the object "Hello" (an array) is lost in our abstract
machine
never to be referred to by an lvalue */

2)

malloc(100);

/* Hereafter the object allocated (100 bytes) is lost, never to be
referred to by an lvalue */




So objects and lvalues are different things.

0
Reply LookSkywalker (71) 2/6/2005 10:08:02 PM

"Romeo Colacitti" <wwromeo@gmail.com> wrote in message
news:1107666467.021755.326460@c13g2000cwb.googlegroups.com...
> Hi,
>
> Does anyone here have a strong understanding for the meanings of the
> terms "lvalue" and "rvalue" as it pertains to C, objects, and different
> contexts?  If so please share.
>
> I've been reading several old posts/threads on the subject, and they
> never end with a conclusion (people keep correcting each other and
> disagreeing).

I always thought an lvalue was something you could take the address of using
&.

So an assignment like:

a=b;

could be rewritten as:

 *(&a)=b;

if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not
an lvalue.

Bart



0
Reply bc (2337) 2/6/2005 10:37:11 PM

Luke Wu wrote:
>

snip

>
> Yes, "lvalues" and "objects" are not the same thing.
>
> An lvalues are just an expression (or the resolution of an
expression)
> that is of a form that can normally be used to designate objects (in
> your case, array[-1] is of a form that is normally used to designate
an
> object, but this specific case is not referring to a valid object -
> thus undefined behaviour).
>
> Objects can also exist without lvalues . For example,
>
> 1)
>
> char *ptr = "Hello";
> ptr = NULL;
>

Even without reassigning ptr to NULL, there still can never exist an
lvalue that maps to the entire string literal object.  The string
literal is an ANONYMOUS object.  Pointers can only point to the
independent char object that make up the string literal aggregate array
(object).

>
> /* Hereafter the object "Hello" (an array) is lost in our abstract
> machine
> never to be referred to by an lvalue */
>
> 2)
>
> malloc(100);
>

The same applies here.  Even if you did capture the return value of
malloc (void *) into a pointer with some sort of reference type, you
will still never get an lvalue that suggests the whole 100 byte object
on the heap (it's an ANONYMOUS object).

Although, you can capture the returned (void *) into a pointer to an
array of size equal to 100 bytes, but that is simply a useless case
(the pointer would serve no purpose).

>
> /* Hereafter the object allocated (100 bytes) is lost, never to be
> referred to by an lvalue */
>
>
>
>
> So objects and lvalues are different things.

There are normal objects, and anonymous objects.
There are valid lvalues, and invalid lvalues.

Only the former from each sentence above can possibly be part of a
lvalue>object mapping.

0
Reply kenneth.bull (37) 2/6/2005 10:40:48 PM

In article <42069c1e@212.67.96.135>, "Bart C" <bc@freeuk.com> wrote:

> "Romeo Colacitti" <wwromeo@gmail.com> wrote in message
> news:1107666467.021755.326460@c13g2000cwb.googlegroups.com...
> > Hi,
> >
> > Does anyone here have a strong understanding for the meanings of the
> > terms "lvalue" and "rvalue" as it pertains to C, objects, and different
> > contexts?  If so please share.
> >
> > I've been reading several old posts/threads on the subject, and they
> > never end with a conclusion (people keep correcting each other and
> > disagreeing).
> 
> I always thought an lvalue was something you could take the address of using
> &.
> 
> So an assignment like:
> 
> a=b;
> 
> could be rewritten as:
> 
>  *(&a)=b;
> 
> if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not
> an lvalue.

Bitfields can be lvalues, but you can't take the address of a bitfield.
0
Reply christian.bau (880) 2/6/2005 10:52:15 PM

In article 
<christian.bau-0CD778.22521506022005@slb-newsm1.svr.pol.co.uk>,
 Christian Bau <christian.bau@cbau.freeserve.co.uk> wrote:

> In article <42069c1e@212.67.96.135>, "Bart C" <bc@freeuk.com> wrote:
> 
> > "Romeo Colacitti" <wwromeo@gmail.com> wrote in message
> > news:1107666467.021755.326460@c13g2000cwb.googlegroups.com...
> > > Hi,
> > >
> > > Does anyone here have a strong understanding for the meanings of the
> > > terms "lvalue" and "rvalue" as it pertains to C, objects, and different
> > > contexts?  If so please share.
> > >
> > > I've been reading several old posts/threads on the subject, and they
> > > never end with a conclusion (people keep correcting each other and
> > > disagreeing).
> > 
> > I always thought an lvalue was something you could take the address of using
> > &.
> > 
> > So an assignment like:
> > 
> > a=b;
> > 
> > could be rewritten as:
> > 
> >  *(&a)=b;
> > 
> > if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not
> > an lvalue.
> 
> Bitfields can be lvalues, but you can't take the address of a bitfield.

And I forgot: Functions are most definitely not lvalues, but you can 
take the address of a function.
0
Reply christian.bau (880) 2/6/2005 10:53:25 PM

Christian,
You wrote  on Sun, 06 Feb 2005 22:53:25 +0000:

>> > So an assignment like:
>> > a=b;
>> > could be rewritten as:
>> >  *(&a)=b;
>> > if a was a legal lvalue. If you can't then 'a' (whatever it might
>> be) is not an lvalue.
>> Bitfields can be lvalues, but you can't take the address of a
>> bitfield.
CB> And I forgot: Functions are most definitely not lvalues, but you can
CB> take the address of a function.

You also forgot that there are non-modifiable lvalues like those that are 
const-qualified or that have incomplete types.

--
Ivan

Unicals Group -- Your own commercial high-quality C99 front end for US $1
http://unicals.com/own-business.html


0
Reply ikosarev (15) 2/6/2005 11:29:00 PM

In article <36nnhvF50s0mnU1@individual.net>,
 "Ivan A. Kosarev" <ikosarev@online.ru> wrote:

> Christian,
> You wrote  on Sun, 06 Feb 2005 22:53:25 +0000:
> 

-- Re-inserted original post here: 
> > "Romeo Colacitti" <wwromeo@gmail.com> wrote in message
> > news:1107666467.021755.326460@c13g2000cwb.googlegroups.com...
> > > Hi,
> > >
> > > Does anyone here have a strong understanding for the meanings of the
> > > terms "lvalue" and "rvalue" as it pertains to C, objects, and different
> > > contexts?  If so please share.
> > >
> > > I've been reading several old posts/threads on the subject, and they
> > > never end with a conclusion (people keep correcting each other and
> > > disagreeing).
> > 
> > I always thought an lvalue was something you could take the address of using
> > &.
> > 
-- End of re-inserted original post
> >> > So an assignment like:
> >> > a=b;
> >> > could be rewritten as:
> >> >  *(&a)=b;
> >> > if a was a legal lvalue. If you can't then 'a' (whatever it might
> >> be) is not an lvalue.
> >> Bitfields can be lvalues, but you can't take the address of a
> >> bitfield.
> CB> And I forgot: Functions are most definitely not lvalues, but you can
> CB> take the address of a function.
> 
> You also forgot that there are non-modifiable lvalues like those that are 
> const-qualified or that have incomplete types.

Looks like I didn't forget anything; you just snipped the part of the 
original post that I answered to.
0
Reply christian.bau (880) 2/6/2005 11:40:20 PM

Kobu wrote:
> Luke Wu wrote:
> >
> > When an lvalue is used in an "general object context," then the
> lvalue
> > is directly acted upon (no conversion to an rvalue takes place).
> > Examples are & and sizeof.
> >
>
> sizeof takes more than lvalues, consider...
>
> sizeof(int *)
> sizeof('A')
> sizeof(33.029e-3LD)
>
> so are types and constants objects too (note, size of directly taking
> the objects as input above, not just lvalues that refer to the
objects)
>
>
> here is another example
>
> sizeof("String Literal")
>
> here, size is receiving only a pointer to the first element of the
> string ('S'), so its equivalent to: sizeof(char *)
>
> but that's not what we get, sizeof actually returns the size of the
> whole string literal
>
> I don't think sizeof fits cleanly with the theory of lvalues/rvalues.


Sizeof is an operator that can take two types of operands:

- types
- expressions(if lvalue expression, gives the size of the entire object
designated by the lvalue, if rvalue expression, gives the size of
object required to properly hold the rvalue)

When considering the sizeof(exp) syntax's behaviour, one might be
tempted to call this an example of an "object context." I argue that it
incorrect to say that sizeof(exp) is a case of "lvalue/object context."
Even though no lvalue-to-rvalue substitution takes plac for lvalue
expression, the fact that rvalues can be operands too should forbit us
from calling it an "object context." It is neither an "object context"
or a "value context", but rather a "makes no assumptions or
substitutions, operates directly on what you hand it CONTEXT"

This is why sizeof('A') works, not because 'A' is an lvalue or object
(constants are not objects).

0
Reply kenneth.bull (37) 2/6/2005 11:41:38 PM

Romeo Colacitti wrote:
> 
> Hi,
> 
> Does anyone here have a strong understanding for the meanings of the
> terms "lvalue" and "rvalue" as it pertains to C, objects, and different
> contexts?  If so please share.
[...]

The simplest way to think of them would probably be:

    An lvalue can go on the left of an assignment statement, and an
    rvalue can go on the right.  (Hence "l" and "r".)

-- 
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody        | www.hvcomputer.com |                             |
| kenbrody/at\spamcop.net | www.fptech.com     | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>

0
Reply kenbrody (1879) 2/6/2005 11:47:04 PM

"Christian Bau" <christian.bau@cbau.freeserve.co.uk> wrote in message 
news:christian.bau-31BAE1.22532506022005@slb-newsm1.svr.pol.co.uk...
....
>> In article <42069c1e@212.67.96.135>, "Bart C" <bc@freeuk.com> wrote:
>> > I always thought an lvalue was something you could take the address of 
>> > using
>> > &.
....
>> Bitfields can be lvalues, but you can't take the address of a bitfield.
>
> And I forgot: Functions are most definitely not lvalues, but you can
> take the address of a function.

I think I'm still right apart from these two exceptions..

Bart



0
Reply bc (2337) 2/7/2005 12:47:16 AM

Kenneth Bull wrote:
 
> Sizeof is an operator that can take two types of operands:
> 
> - types
> - expressions

The types must be object types 
and the expressions must be expressions of object type.

-- 
pete
0
Reply pfiland (6614) 2/7/2005 1:16:48 AM

Christian Bau wrote:
> 
> In article
> <christian.bau-0CD778.22521506022005@slb-newsm1.svr.pol.co.uk>,
>  Christian Bau <christian.bau@cbau.freeserve.co.uk> wrote:
> 
> > In article <42069c1e@212.67.96.135>, "Bart C" <bc@freeuk.com> wrote:
> >
> > > "Romeo Colacitti" <wwromeo@gmail.com> wrote in message
> > > news:1107666467.021755.326460@c13g2000cwb.googlegroups.com...
> > > > Hi,
> > > >
> > > > Does anyone here have a strong understanding for
> > > > the meanings of the
> > > > terms "lvalue" and "rvalue" as it pertains to C,
> > > > objects, and different
> > > > contexts?  If so please share.
> > > >
> > > > I've been reading several old posts/threads on the subject,
> > > > and they
> > > > never end with a conclusion
> > > > (people keep correcting each other and
> > > > disagreeing).
> > >
> > > I always thought an lvalue was something you could
> > > take the address of using
> > > &.
> > >
> > > So an assignment like:
> > >
> > > a=b;
> > >
> > > could be rewritten as:
> > >
> > >  *(&a)=b;
> > >
> > > if a was a legal lvalue.
> > > If you can't then 'a' (whatever it might be) is not
> > > an lvalue.
> >
> > Bitfields can be lvalues,
> > but you can't take the address of a bitfield.
> 
> And I forgot: Functions are most definitely not lvalues, but you can
> take the address of a function.

register qualified variables are lvalues without addresses.

-- 
pete
0
Reply pfiland (6614) 2/7/2005 1:21:52 AM

Kenneth Bull wrote:
>
> There are normal objects, and anonymous objects.
>

A search for the term - anonymous - in the C standard came up with 0
results.

Explanation for malloc does say that it allocates AN OBJECT.
Explanation for calloc does say that it allocates AN ARRAY OF OBJECTS.
No mention of anonymous though.

I've come to the conclusion that the C language (and it's standard) has
so many exceptions, loopholes and gray areas that it's better to learn
and see all the cases than to try to understand the definitions and
rules within the standard.

I bet every C programmer has a different idea for what all these terms
mean, but all experts have seen all the cases/uses enough to understand
things deep enough not to care for exact definitions. Such a
frustrating language, but I can't live without it :-)

0
Reply wwromeo (17) 2/7/2005 5:27:18 AM

On Sun, 06 Feb 2005 14:08:02 -0800, Luke Wu wrote:

....

> Objects can also exist without lvalues . For example,
> 
> 1)
> 
> char *ptr = "Hello";
> ptr = NULL;
> 
> /* Hereafter the object "Hello" (an array) is lost in our abstract
> machine
> never to be referred to by an lvalue */

The string literal itself i.e. the "Hello" in the source code is an lvalue.

> 2)
> 
> malloc(100);
> 
> /* Hereafter the object allocated (100 bytes) is lost, never to be
> referred to by an lvalue */

True, but not very interesting or useful.

> So objects and lvalues are different things.

lvalues exist in the source code, objects exist in the execution
environment.

Lawrence
0
Reply lknews (877) 2/7/2005 11:27:51 AM

On Sun, 06 Feb 2005 09:04:38 -0800, Kobu wrote:

> 
> Luke Wu wrote:
>>
>> When an lvalue is used in an "general object context," then the
> lvalue
>> is directly acted upon (no conversion to an rvalue takes place).
>> Examples are & and sizeof.
>>
> 
> sizeof takes more than lvalues, consider...
> 
> sizeof(int *)
> sizeof('A')
> sizeof(33.029e-3LD)
> 
> so are types and constants objects too (note, size of directly taking
> the objects as input above, not just lvalues that refer to the objects)
> 
> 
> here is another example
> 
> sizeof("String Literal")
> 
> here, size is receiving only a pointer to the first element of the
> string ('S'), so its equivalent to: sizeof(char *)

Why do you assume that to start with? Note that while you are using a
string literal here, it is just an example of an array lvalue.

> but that's not what we get, sizeof actually returns the size of the
> whole string literal
> 
> I don't think sizeof fits cleanly with the theory of lvalues/rvalues.

sizeof is unique because it doesn't evaluate its operand (as a value or an
lvalue); all sizeof cares about is the type of the operand. With C99 VLAs
determining the operand type can require a calculation based on runtime
information but it still isn't accessing the value or lvalue of the
operand (and obviously not if the operand is specifically a type).

Lawrence

0
Reply lknews (877) 2/7/2005 11:45:09 AM

"Romeo Colacitti" <wwromeo@gmail.com> writes:
> Chris Torek wrote:
>> In article <4205BD5C.6DC8@mindspring.com>
>> pete  <pfiland@mindspring.com> wrote:
[...]
>> >If you have
>> >    int array[1];
>> >then
>> >    array[-1] is an example of an lvalue which doesn't refer
>> >to an object. The use of such an lvalue would be undefined behavior.
>>
>> Again, apparently true in C99, but not (technically) in C89.  But
>> this just means the C89 standard has a defect.
>>
>
> C99 is worse, because as per the said standard almost every expression
> that doesn't resolve to an incomplete types or function type is an
> LVALUE.  So something like 2+3 is an lvalue in C99.  I'll just modify
> that errant sentence in the C99, it must obviously be a typo (they
> forgot to mention that an lvalue "designates an object.")

Alas, it wasn't a typo.  In C90, an lvalue is defined as "an
expression (with an object type or an incomplete type other than void)
that designates an object".  The flaw in this definition, given
    int *ptr;
the expression *ptr may or may not designate an object (it doesn't of
ptr==NULL, for example).  So strictly speaking, you can't always
determine whether an expression is an lvalue until runtime, which
clearly was not the intent.

C99 attempted to correct this by dropping the absolute requirement
that an lvalue must designate an object; an lvalue is now "an
expression with an object type or an incomplete type other than void;
if an lvalue does not designate an object when it is evaluated, the
behavior is undefined."  Strictly speaking, 42 is an lvalue (it's an
expression with an object type) -- and evaluating it invokes undefined
behavior.  Again, this clearly was not the intent.

An lvalue *should* be defined as an expression that *potentially*
designates an object.  For example, *ptr is an lvalue regardless of
the value of ptr, and evaluating *ptr invokes undefined behavior if it
doesn't currently designate an object.  The expression 42 is not an
lvalue.  The problem, I suppose, is expressing this in standardese,
but IMHO correctness is more important than rigor here.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
Reply kst-u (21963) 2/7/2005 9:28:30 PM

Keith Thompson wrote:
>
> An lvalue *should* be defined as an expression that *potentially*
> designates an object.  For example, *ptr is an lvalue regardless of
> the value of ptr, and evaluating *ptr invokes undefined behavior if
it
> doesn't currently designate an object.  The expression 42 is not an
> lvalue.  The problem, I suppose, is expressing this in standardese,
> but IMHO correctness is more important than rigor here.
>

Or they could just list all possible types of lvalues in a short
passage (there aren't many).  They would have to define it according to
the final result of expressions (what they evaluate to), which is what
they mean by "expressions" in the current standard anyway.

0
Reply kenneth.bull (37) 2/8/2005 10:02:26 PM

Lawrence Kirby wrote:

> sizeof is unique because it doesn't evaluate its operand (as a value
or an
> lvalue); all sizeof cares about is the type of the operand. With C99
VLAs
> determining the operand type can require a calculation based on
runtime
> information but it still isn't accessing the value or lvalue of the
> operand (and obviously not if the operand is specifically a type).
>
> Lawrence

Which is why people shouldn't call sizeof an "object context" or
"lvalue context"

0
Reply kenneth.bull (37) 2/8/2005 10:07:52 PM
comp.lang.c 29411 articles. 31 followers. Post

24 Replies
96 Views

Similar Articles

[PageSpeed] 36


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

What exactly is lvalue & rvalue (old c.l.c. posts are all over the map)?
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been reading several old posts/threads on the subject, and they never end with a conclusion (people keep correcting each other and disagreeing). My take on it is that an "lvalue" is an expression that refers to an object (which can have (a) value(s) within it), and "rvalue" is an expression that only has a value (ephemeral value as Chris Torek would claim) and no associ...

Binary vs integer: which is preferable? (X-post on c.constraints and c.l.prolog)
Dear all, I've a modeling problem that i wish to submit to your experience. Briefly, I've a constraint which could be expressed in two different ways: 1) X2 > X1 + sum(m){Ym * Tm} sum(m) {Ym} = 1 where: X1 and X2 are integer variables Ym is an array of boolean variables Tm is an array of constants m belongs to (1,2,...,M) 2) X2 > X1 + Tm m = X_m where: X1, X2 and X_m are integer variables (X_m belongs to (1,2....,M)) Tm is an array of constants therefore the index m is an integer that belongs to (1,2....,M) The question is: from a theoretical point of view, which ...

READ THIS B4 POSTING IN COLA. C.O.L.A Newcomer FAQ (Read This Before Posting in COLA)
C.O.L.A. Newcomer FAQ and Primer Edition: 14.079.0712.91P - 7/4/05 Group: comp.os.linux.advocacy Copyright (c) 2002-2005 Linux Reality Team Welcome to comp.os.linux.advocacy, otherwise known as cola. This FAQ will try to address most of the issues regarding Linux and this group. Unlike the other FAQs, this one will try to be as realistic as possible. If you want the straight information from real people, continue reading. If you would like to be told what you want to hear, or read a bunch of misinformation that you will regret later as you find things don't work as they should, feel free...

c.l.r posts lost?
Hi, I'm reading the newsgroup, and i seem to miss some posts. Eg, I saw Nicholas Van Weerdenburg's answer to the Nitro + Og 0.9.3 announcement, but I haven't received George Moschovitis' announcement. It's been some weeks I experience this, seeing a reply to a post I didn't get. Anyone else having this problem? I know there were problems with the ML<->c.l.r gateway in the past, and wonder if this is related. thanks. Raph Raphael Bauduin ha scritto: > Hi, > > I'm reading the newsgroup, and i seem to miss some posts. Eg, I saw > Nicholas V...

Searching old c.s.a.* postings
To search the google archives of all the comp.sys.acorn newsgroups I used to use: groups.google.co.uk/advanced_search�?q=group:comp.sys.acorn.* they now changed the URL to: groups.google.co.uk/advanced_search�?q=group:comp.sys.acorn.apps But they no longer accept the * wild card for any group insisting on specific group e.g. comp.sys.acorn.apps The above is on a PC as Google now insist on javascript! It also appears that you can't be signed into a google account or you have to have the groups you want to search in 'your groups' n.b. At this rate of 'imp...

australian access to C.L.F. posts
For those Australian residents who read or post and don't want Googled SPAM, the main service supplier on this continent has a problem with its newreader service (trying to mix video streaming and usenet?). I just signed up for (free) eternal september, which has about 6000 news feeds including all the professional and other programming Usenet offers. I now caught up with all you other guys in one vast 4800 + message update. H'm. Sstill have to add 'casinos' to my delete list... Terence Well done Terence. Now, why didn't you try that 3 years ago when I...

How I got rid of those spam posts on c.l.t
Hi, If like me you are using Google Groups with Firefox, and are suffering from the huge amount of spam posts, maybe you'll like this little trick: use the fabulous Firefox extension "GreaseMonkey", which is a generic filter allowing to run arbitrary Javascript code (possibly modifying the contents) after loading webpages. Using the GreaseMonkey script located at http://paste.tclers.tk/887 you'll see that all those "Gucci ... Replica .. whatever" garbage lines are replaced by whitespace in the Topic List view of Google Groups. So far that's t...

Posting issues (Google Groups + c.l.t) ?
Does anybody get repeated Google Groups failures to post on comp.lang.tcl ? -Alex ...

Etymology of MAP[LIST,CAR,CON,CAN,C,L]
So I've been thinking a lot about cons cells and the relation between the various mapping functions in Common Lisp. Poking around in the Google archives of c.l.l. and in various Lisp manuals I have lying around, I put together this chart: Common Lisp | MAPLIST | MAPCON | MAPL | MAPCAR | MAPCAN | MAPC | -------------------------------------------------------------------- Lisp 1.5 | MAPLIST | MAPCON | MAP | - | - | - | Interlisp* | MAPLIST | MAPCON | MAP | MAPCAR | MAPCONC| MAPC | ZetaLisp | MAPLIST | MAPCON | MAP | MAPCAR | MAPCAN | MAPC | T ...

C.O.L.A Newcomer FAQ (Read This Before Posting in COLA)
C.O.L.A. Newcomer FAQ and Primer Edition: 14.079.0712P - 7/4/03 Group: comp.os.linux.advocacy Copyright (c) 2002-2003 Linux Reality Team Welcome to comp.os.linux.advocacy, otherwise known as cola. This FAQ will try to address most of the issues regarding Linux and this group. Unlike the other FAQs, this one will try to be as realistic as possible. If you want the straight information from real people, continue reading. If you would like to be told what you want to hear, or read a bunch of misinformation that you will regret later as you find things don't work as they should, feel fr...

s u b j e c t : L o w c o s t m e d i c a l a n d m e d i c i n e f r o m C h i n a
s u b j e c t : L o w c o s t m e d i c a l a n d m e d i c i n e f r o m C h i n a U n i V i s u a l G r o u p I n c . M o n t h l y N e w s - - - August E d i t i o n ########################################################## U L T R A S O U N D ......................... $ 6 5 0 P A C S ........................... $ 1 0 0 0 H I S ............................. $ 1 0 0 0 P a t i e n t M o n i t o r .................. $ 1 28 0 C a b l e E C G ............................ $ 9 9 9 M i c r o s c o...

c.l.c charter
Where can I view an official copy of c.l.c's charter? (I need it to help report the nutjobs who spam here on occasion...) -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome. Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote in news:bqklr4$f4m$1@chessie.cirr.com: > Where can I view an official copy of c.l.c's charter? (I need it to > help report the nutjobs who spam here on occasion...) I believe clc predates charters, see the thread at http://tinyurl....

Map C++ feature to C
Dear friends Is there a way to embulate C++'s virtual destructors in pure C. IE just using function pointers. Many Thanks. On 06/24/12 08:23 PM, zgene wrote: > Dear friends Why is the question completely different form the subject? > Is there a way to embulate C++'s virtual destructors in pure C. IE just > using function pointers. No, because there isn't a way to fully emulate destructors. -- Ian Collins On Jun 24, 9:23=A0am, zgene <nos...@nospam.com> wrote: > Dear friends > > Is there a way to embulate C++'s virtual destru...

Newsreaders and c.l.c.
What is a good newsreader (cheap and quick) for c.l.c.? I quit using dbforums last year because of delays in posting. I switched to Google and it now takes almost a day to see a message that I post. Today I read on dbforums a thread about Clipper and Linux with 6 posts entered beginning January! None of these posts made it to Google! Before dbforums, I used the Prodigy newsreader and it posted almost immediately. I dropped Prodigy after they were bought by SBC and I lost some business emails. I have AOL for dialup but use the enhanced Hotmail for most of my email. Thanks, Mike PS - th...

some c.l.c. oldies
Kaz on goto, August 1996, message ID <4vknm4$pqa@latte.cafe.net>: https://groups.google.com/forum/#!original/comp.lang.c/KCSpJE0KF0k/DmeMM1PcgJAJ Kaz on large numbers of parameters and globals, Dec 1996, Message ID <58n4ln$96f@bcrkh13.bnr.ca>: https://groups.google.com/forum/#!original/comp.lang.c/JH_XjmBJ6U8/vYmvYyqp15QJ Kaz disagrees with with pedant Dan Pop that C has no "activation records". November 1996, message ID <56t456$pe3@bcrkh13.bnr.ca>: https://groups.google.com/forum/#!original/comp.lang.c/fO1R4NrAQ9w/g7nu1rzbOQgJ (However, I hasten...

Up-to-dateness of the c.l.c. FAQ
Hi all, Isn't it time for an update of the wording in some sections of the c.l.c. FAQ ? The last update has been in 1999, I think, and quite a bit that has happened since then should probably be addressed (introduction of C99 final, for one thing). Also, it would be a good idea to synchronise the text version with the freely available HTML and printable versions. As to the FAQ text, I see it is copyrighted by Steve Summit, and there's a book version available as well. I appreciate the efforts he undoubtedly has put into this, but shouldn't this kind of thing properly bel...

returning lvalue in C vs C++
Can anyone explain this: struct thing { int x; }; struct thing f(void) { struct thing ret = {100}; return ret; } int g(void) { return 10; } int main(void) { #if 0 g() = 4; /* "invalid lvalue" in C and C++ */ #endif f().x = 200; /* "invalid lvalue" in C, but not in C++ apparently */ return 0; } $ gcc -x c++ main.c no problems in C++, but $ gcc -x c main.c main.c: In function ‘main’: main.c:17: error: invalid lvalue I'm using gcc version 4.0.2 20050901. Ben C wrote: > f().x = 200;    /*...

Standard C++ and old-C++ headers
Hi, Does the C++ standard require implementations of C++ to contain any of the "old" header files and their functionalities? (eg stdio.h, iostream.h, fstream.h) If so, which ones? Thanks, James McLaughlin. zeNoTTOBESpammeDrotype@yahoo.com wrote: > Does the C++ standard require implementations of C++ to contain any of > the "old" header files and their functionalities? (eg stdio.h, > iostream.h, fstream.h) > > If so, which ones? <stdio.h> and other 'C90' headers, declarations for C Standard Library, are supposed to be there. There are 18 ...

c.l.j.programmer vs. c.l.j.help (was: Re: What does this mean??)
Roedy Green <my_email_is_posted_on_my_website@munged.invalid> wrote on Wed, 02 Nov 2005 14:18:39 GMT in comp.lang.java.programmer: >On Wed, 02 Nov 2005 13:53:56 GMT, "Thomas G. Marshall" ><tgm2tothe10thpower@replacetextwithnumber.hotmail.com> wrote, quoted >or indirectly quoted someone who said : > >>However, if people here are going to get all pissy about >>toward newbies and where they belong, then I will in their defense get >>equally pissy and point out the descriptions of the newsgroups established >>by the closest thing there ...

Q: Old development systems, LightSpeed C 3.01, Think C 4.0, Symantec C, CW-tools
Hello to everyone ! Does anybody know wehere to get those old very, very old development systems. The older the better. I'm specially interested in the LightSpeed C 3.01. Does anybody know what is the legal status of copying to packages ? -Asko Eerola, Helsinki (mail here or use First.Lastname@iki.fi and remember to replace the fields in the address) Hi, Found I have Think C 2.0, 4.0 and 5.0 plus Think C Reference. The last is a very nice Mac OS API reference (almost better than what's in X code). --jim Hi. What are you trying to do with these old developm...

[OT] Spam in c.l.c.m
Which moderator allows the spam messages in comp.lang.c.moderated? I thought half the point of moderation was to avoid such messages. In case you don't know what I'm talking about, recent messages include: 17-Apr: Call for an Impeachment Inquiry of Bush and Cheney 16-Apr: Call for an Impeachment Inquiry of Bush and Cheney 15-Apr: Hey, Corporate America! Show Taxpayers Some Apprecia... 10-Apr: Statement on Spam and VoteNader.org oldwolf@inspire.net.nz (Old Wolf) writes: > Which moderator allows the spam messages in comp.lang.c.moderated? I thought > half the point of...

Microsoft C# a replacement for old C++ language
I heard from a friend that C++ is mainly being replaced as language of choice for most C++ programmers around the world. I guess this makes sence as it means that when learning you dont have to fuck around sencelessly with low level garbage like pointers. I was going to learn C++ but if times are changing I think I will just learn C#, it seems more modern. I also thought about Java but its WAY too slow. I think it should be easy to learn as I have heaps of experience making complex programs using all versions of Visual Basic. Any comparisons out there? Thanks The Grand Master Master Prog...

Microsoft job posting
Microsoft - Senior Analytics Scientist http://www.datashaping.com/jobs.shtml vincentg@datashaping.com writes: > Microsoft - Senior Analytics Scientist > http://[deleted] This is a technical discussion group, not a jobs board. Use one of the many newsgroups with "jobs" in their names. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. ...

Job posting for C,C++ requirement in Noida
Hi!!!!!!!!!!! We are company in Hyderabad, extensively looking for C,C++ professionals. Details of the requirement : Location : Noida. Experience : 3 years to 8 years. We have Walk-In event happening on 10th & 11th February in Banglore. If you are interested in the above requirement and able to attend the pesonal interview in Banglore,please forward your updated profile to sowjanya.avala@cesltd.com. please forward this mail to your freinds or colleagues who are looking for the same kind of oppurtunity,so that it reaches the concerned person. Thanks & Rega...

C++ Wrapper 4 OLD C Library??
I'm looking at creating a wrapper C++ object as a front end for an older C library, also the library is not thread-safe, which I have to somehow make safe for multi-threading (CRITICAL_SECTIONS maybe) for integration into a server. Anyone have any thoughts or standard approaches to doing something like this? Thanks, Ian mc_ian@yahoo.com ...