The value of (a == b) 
Author Message
 The value of (a == b)

Quote:
Charles Bloom writes:
> > Oh well.. I'll use "a += (a==a)" to increment variables from now on :^)
Aaron Crane writes:
> I can't work out whether or not that's legal. ...

It is.

Quote:
> Does the (a==a) constitute an access which will "only determine the value to
> be stored"?

Which is only *to* determine the value to be stored.  Yes, it is.
--
Mark Brader, Toronto  | Keep out of eyes--if this occurs, rinse with water.

My text in this article is in the public domain.
--



Fri, 23 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Only once "a" is changed here i.e. in a+=.
a==a cannot change value of a.



Quote:


> > Oh well.. I'll use "a += (a==a)" to increment variables from now on
:^)

> I can't work out whether or not that's legal.  The standard says:

> | Between the previous and next sequence point an object shall have
its
> | stored value modified at most once by the evaluation of an
expression.
> | Furthermore, the prior value shall be accessed only to determine the
value
> | to be stored.

> Does the (a==a) constitute an access which will "only determine the
value to
> be stored"?

> --


<URL:http://pobox.com/~aaronc/>

Quote:
>  ** Please send on-topic followups by Usenet, not email **
> --


--
---------------------------
Raja

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
--



Fri, 23 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:

>Only once "a" is changed here i.e. in a+=.
>a==a cannot change value of a.

That's not the point.  If you change the value of 'a' you're not allowed to
look at the value of 'a', even if you don't change it again, without an
intervening sequence point.  So something like 'foo[a] = a++' is forbidden.

Were it allowed, there would have to be a rule about whether you got the old
or the new value of 'a', and the compiler would be restricted in the
optimisations it could use because it would have to follow that rule.

The exception is if the looked-at value of 'a' is used to calculate the new
value of 'a'.  This exception makes expressions like 'a = a + 1' legal.  In
this case it's clear that the value of 'a' required must be the old one
(because the new one isn't known yet) and, moreover, there is no possible
optimisation which would want to have it the other way round.

Cheers,
Richard
--



Sat, 24 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:

> All ones is only -1 if the representation used for negative integers is
> two's-complement. The c standard doesn't specify that it has to be so.

A long, long time ago, I used to have

#define FALSE (0)
#define TRUE (~0)

IE, TRUE is all bits 1. Theory being that it doesn't matter if you use
bitwise or boolean operators.

Thing is, under a one's complement CPU, there are two representations of
zero, all bits 0 and all bits 1.

#define FALSE (0)
#define TRUE (~0)
typedef int bool_t;
....
bool_t a=FALSE;
bool_t b=TRUE;
.....
if (a==b)

Not good advice above. Don't do that.

Bill, a better programmer than he was yesterday, but not as good as
he will be tomorrow.

--



Tue, 27 Nov 2001 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software