Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > lvalue -modifiable and non-modifiable

Reply
Thread Tools

lvalue -modifiable and non-modifiable

 
 
Kavya
Guest
Posts: n/a
 
      11-06-2006
Can someone give and explain in simple terms a definition of lvalue?
Also what are these modifiable and non-modifiable lvalues?
I always thought that, if we can assign to anything then that anything
is lvalue and if cannot assign to anything then that anything is not
lvalue.

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      11-06-2006
"Kavya" <(E-Mail Removed)> wrote:

> Can someone give and explain in simple terms a definition of lvalue?


Sure. The Standard can, in [6.3.2.1#1]:

# An lvalue is an expression with an object type or an incomplete type
# other than void;

> Also what are these modifiable and non-modifiable lvalues?


From the same bit of the Standard:

# A modifiable lvalue is an lvalue that does not have array type, does
# not have an incomplete type, does not have a const-qualified type, and
# if it is a structure or union, does not have any member (including,
# recursively, any member or element of all contained aggregates or
# unions) with a const-qualified type.

> I always thought that, if we can assign to anything then that anything
> is lvalue and if cannot assign to anything then that anything is not
> lvalue.


That's a common use of the term, but not as it's used in the ISO C
Standard. It's questionable if this was a good idea, but that's what we
have to work with.

Richard
 
Reply With Quote
 
 
 
 
Kavya
Guest
Posts: n/a
 
      11-06-2006

Richard Bos wrote:
> "Kavya" <(E-Mail Removed)> wrote:
>
> > Can someone give and explain in simple terms a definition of lvalue?

>
> Sure. The Standard can, in [6.3.2.1#1]:
>
> # An lvalue is an expression with an object type or an incomplete type
> # other than void;
>
> > Also what are these modifiable and non-modifiable lvalues?

>
> From the same bit of the Standard:
>
> # A modifiable lvalue is an lvalue that does not have array type, does
> # not have an incomplete type, does not have a const-qualified type, and
> # if it is a structure or union, does not have any member (including,
> # recursively, any member or element of all contained aggregates or
> # unions) with a const-qualified type.
>
> > I always thought that, if we can assign to anything then that anything
> > is lvalue and if cannot assign to anything then that anything is not
> > lvalue.

>
> That's a common use of the term, but not as it's used in the ISO C
> Standard. It's questionable if this was a good idea, but that's what we
> have to work with.



Thank You Very Much.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-06-2006
"Kavya" <(E-Mail Removed)> writes:
> Can someone give and explain in simple terms a definition of lvalue?
> Also what are these modifiable and non-modifiable lvalues?
> I always thought that, if we can assign to anything then that anything
> is lvalue and if cannot assign to anything then that anything is not
> lvalue.


Briefly, an lvalue is an expression that designates an object.
Unfortunately, it's not quite that simple, and both C90 and C99 have
seriously flawed definitions of "lvalue".

Here's the definition from the C90 standard, C90 6.2.2.1p1:

An _lvalue_ is an expression (with an object type or an incomplete
type other than void) that designates an object. When an object is
said to have a particular type, the type is specified by the
lvalue used to designate the object. A _modifiable lvalue_ is an
lvalue that does not have array type, does not have an incomplete
type, does not have a const-qualified type, and if it is a
structure or union, does not have any member (including,
recursively, any member of all contained structures or unions)
with a const-qualified type.

with a footnote similar to the one in the C99 standard (see below).

The C90 standard's definition had a fairly serious problem. If the
above definition is taken literally, then we can't necessarily
determine whether a given expression is an lvalue until we know its
value at execution time. This is clearly unacceptable; the compiler
needs to know *during compilation* whether an expression in certain
contexts is an lvalue or not. It also clearly was not the intent.
C90's definition failed to capture the idea that if p is a pointer, *p
doesn't cease to be an lvalue if p == NULL.

Here's the definition from the C99 standard, C99 6.3.2.1p1:
An _lvalue_ is 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. When an object
is said to have a particular type, the type is specified by the
lvalue used to designate the object. A _modifiable lvalue_ is an
lvalue that does not have array type, does not have an incomplete
type, does not have a const-qualified type, and if it is a
structure or union, does not have any member (including,
recursively, any member or element of all contained aggregates or
unions) with a const-qualified type.

with a footnote:
The name "lvalue" comes originally from the assignment expression
E1 = E2, in which the left operand E1 is required to be a
(modifiable) lvalue. It is perhaps better considered as
representing an object "locator value". What is sometimes called
"rvalue" is in this International Standard described as the "value
of an expression".

The C99 standard attempted to correct this flaw by dropping the
wording that an lvalue must designate an object. But designating an
object is the whole idea of what an lvalue is. The cure was worse
than the disease.

Reading the C99 definition literally, the integer constant 42 is an
lvalue, since it's an expression with an object type (namely int), and
evaluating that expression *in any context* invokes undefined behavior
since it does not designate an object.

Clearly this was not the intent either.

I don't believe it's impossible to interpret either definition
literally to yield a consistent description of the language.

An lvalue is an expression that *potentially* designates an object.
It may do so unconditionally (for example, an object name is always an
lvalue), or it may do so conditionally (for example, if p is a
pointer, then *p is an lvalue regardless of whether it *currently*
designates an object or not). 42 is not an lvalue because it cannot
ever designate an object. If an lvalue does not designate an object
when it's evaluated, the behvior is undefined.

This has been discussed at length in comp.std.c.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
lvalue difference between static and non static member functions tkrogc@gmail.com C++ 9 02-06-2006 02:06 PM
const and non-const operator[] Vs. lvalue and rvalue use Mark Stijnman C++ 2 04-22-2005 02:32 PM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM



Advertisments