Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to an int array

Reply
Thread Tools

pointer to an int array

 
 
Al Bowers
Guest
Posts: n/a
 
      01-15-2005


Keith Thompson wrote:
> Lawrence Kirby <(E-Mail Removed)> writes:
> [...]
>
>>Dereferencing a pointer can yield an lvalue. However the text of the
>>standard looks terminally broken in this respect. I should probably check
>>defects.

>
>
> Yes, the standard's definition of "lvalue" is badly broken.
>
> C90 6.2.2.1 says:
>
> An _lvalue_ is an expression (with an object type or an incomplete
> type other than void) that designates an object.
>
> The problem with this definition is that, strictly speaking, you can't
> determine at compilation time whether something is an lvalue or not.
> Given a declaration
>
> int *ptr;
>
> the expression *ptr designates an object only if current the value of
> ptr is non-null.


A value of non-null doesn't assure *ptr designate an object.

Given:
int *ptr = malloc(0);
ptr need not have the value of NULL.
So, is *ptr an object where ptr a value of NULL or some other
implementation defined value? Either way, *ptr cannot be used to
access an object.

--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-15-2005
Al Bowers <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> Lawrence Kirby <(E-Mail Removed)> writes:
>> [...]
>>
>>>Dereferencing a pointer can yield an lvalue. However the text of the
>>>standard looks terminally broken in this respect. I should probably check
>>>defects.

>> Yes, the standard's definition of "lvalue" is badly broken.
>> C90 6.2.2.1 says:
>> An _lvalue_ is an expression (with an object type or an
>> incomplete
>> type other than void) that designates an object.
>> The problem with this definition is that, strictly speaking, you
>> can't
>> determine at compilation time whether something is an lvalue or not.
>> Given a declaration
>> int *ptr;
>> the expression *ptr designates an object only if current the value of
>> ptr is non-null.

>
> A value of non-null doesn't assure *ptr designate an object.
>
> Given:
> int *ptr = malloc(0);
> ptr need not have the value of NULL.
> So, is *ptr an object where ptr a value of NULL or some other
> implementation defined value? Either way, *ptr cannot be used to
> access an object.


Right, which is why I wrote "only if" above, not "if and only if"
(though I could have been more specific).

The point is that "lvalue" is intended to be a compile-time concept,
so *ptr should be considered an lvalue regardless of the current value
of ptr.

--
Keith Thompson (The_Other_Keith) (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
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      01-16-2005
Lawrence Kirby <(E-Mail Removed)> wrote:
> On Thu, 13 Jan 2005 22:21:41 +0000, S.Tobias wrote:
> > Lawrence Kirby <(E-Mail Removed)> wrote:
> >> On Wed, 12 Jan 2005 00:00:17 +0000, S.Tobias wrote:


> >> > # 2 The operand of the unary * operator shall have pointer type.
> >> > a) This clearly allows pointers to incomplete types; why aren't they
> >> > generally forbidden?

> >
> > Dereferencing a pointer yields an lvalue.


> Dereferencing a pointer can yield an lvalue.

[snip]

And when it doesn't?

> Essentially the problem is that the concept of lvalue is used in
> constraint specifications (e.g. 6.5.16p2) which means that it is a
> property that must be determinable at compile time, i.e. from static
> analysis of the source code. C99 6.5.3.2p4 says


> "The unary * operator denotes indirection. If the operand points to a
> function, the result is a function designator; if it points to an object,
> the result is an lvalue designating the object."


> This is a specification for lvalue-ness based on a runtime property i.e.
> whether the pointer points at an object or not, which makes it unworkable.


I understand your point. However, on second thought, this is not a
definition. What is says is that `*p' 1. designates the object `p'
points to (and not some other), and 2. is an lvalue (the expression can
potentially exist in any position where an lvalue is expected).

If `p' does not point to any valid object, `*p' raises UB (because
the Standard fails to define it); past this point there is no need to
consider whether `*p' is an lvalue or not, the behaviour is undefined
anyway. (The case of &*p has been covered in earlier section.)

> C99 6.3.2.1 says:


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


This is the definition. Lvalue is compile-time concept.

> Note specifically that lvalues can have an incomplete type which is good
> and correct. This of course has its own problems, apparently saying that
> the expression 42 is an lvalue.


> > Since incomplete type
> > doesn't fully describe an object, the result can't be a "good lvalue".


I wrote this because my idea of lvalue was not the same as in the
Standard. Thanks for pointing this out to me (I also thank Keith
Thompson for his post). I start to feel that the concept of lvalue
is not only semantical (object designator), but also it is a "label"
that takes part in grammatical analysis of expressions; maybe this is
the reason why certain rules seem a little confusing.

I think I need to stop right here and think things over again, I really
can't add anything productive at this point. Discussing with you has
been very enlightening for me, thanks a lot!



struct mystruct *p;
*p; //gcc refused to compile

The last sentence in 6.3.2.1p2 (incredible how I selectively read that
text) says, referring to lvalues not being an operand to unary & and
other operators:
# If the lvalue has an incomplete type and does not have array type,
# the behavior is undefined.
which covers my previous question. However, I wouldn't like a compiler
that would issue only a warning.

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
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
what is it for an array ? int f(int VAR) { int tab[VAR]; .. } Alain Spineux C Programming 6 05-17-2011 01:03 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments