Relational operator outside loops and conditionals..

J

Janus

Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

..
..
..
state == 2;
..
..

Will the statement be ignored (or as good as ignored) all the times?



Thanks,
Deepak
 
R

Richard Heathfield

Janus said:
Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

.
.
.
state == 2;
.
.

Will the statement be ignored (or as good as ignored) all the times?

In this case, assuming 'state' is what it appears to be, the compiler is
very likely to ignore it completely, although a good compiler will probably
diagnose it - "code has no effect" or something along those lines.

But if you had:

state == function();

that would be a different matter, since function() may have side effects.
 
E

Eric Sosman

Janus wrote On 08/09/06 12:18,:
Hi,

This is a very basic question.

What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...

.
.
.
state == 2;
.
.

Will the statement be ignored (or as good as ignored) all the times?

The expression is evaluated, and the result of the
evaluation is discarded. The compiler may (or may not)
"notice" that the evaluation makes no difference to the
behavior of the program, and then decide to eliminate it.
If the evaluation has no effect, there is no way for the
program to discover whether it was or was not performed.

There's nothing special about the relational operators
in this regard. Here are some other expressions to ponder:

x + y;
c ? x : y;
x;

.... and, of course

state++ == 2;
 
J

Janus

Thanks a lot for the reply.
I was using an enum value on Right hand side, and the == was a typo
actually and was not solving my issue. So I was just curious.

Thanks once again,
Deepak
 
M

Morris Dovey

Janus (in (e-mail address removed)) said:

| This is a very basic question.
|
| What will happen if there is a statement with a relational operator
| outside while, for, or if conditions?
|
| Will the statement be ignored (or as good as ignored) all the times?

The operator will work as expected. One of my favorite examples:

int sign;
:
sign = (x > 0) - (x < 0);

sets the variable sign to 1, 0, or -1 if x is positive, zero, or
negative.
 
R

Richard Tobin

Janus said:
What will happen if there is a statement with a relational operator
outside while, for, or if conditions?

Something like this...
.
state == 2;

The important thing to realise - as you seem to have done - is
that == is just an operator, so this statement is not really any
different from

3+4;

Not all languages work like this. In some languages comparisons are
(or were, I don't think many modern languages do it) part of the
syntax of conditionals.

-- Richard
 
K

Kenneth Brody

Eric said:
Janus wrote On 08/09/06 12:18,: [...]
state == 2;
[...]
There's nothing special about the relational operators
in this regard. Here are some other expressions to ponder:

x + y;
c ? x : y;
x;

... and, of course

state++ == 2;

I assume that:

state++ == state++;

is still UB, even though there's no assignment?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Kenneth Brody wrote On 08/10/06 11:32,:
I assume that:

state++ == state++;

is still UB, even though there's no assignment?

Right: two modifications to the same object without
an intervening sequence point -> unbehaved definior.
 
R

Richard Heathfield

Kenneth Brody said:

I assume that:

state++ == state++;

is still UB, even though there's no assignment?

Yes, it's still UB, because:

"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."
 
S

Serve Laurijssen

Eric Sosman said:
x + y;
c ? x : y;
x;

... and, of course

state++ == 2;

what about in this case? Is the compiler allowed to not execute the pointer
dereference?

int main(void) {

char *p = some_address();


*p == 3;

return 0;

}
 
E

Eric Sosman

Serve Laurijssen wrote On 08/10/06 14:34,:
what about in this case? Is the compiler allowed to not execute the pointer
dereference?

int main(void) {

char *p = some_address();


*p == 3;

return 0;

}

Yes, the compiler can omit the useless comparison.

Argument: If the pointer is valid and the data it points
to is valid and "everything is quite correct," the expression
has no effect. If the pointer is invalid or the object it
points to holds a trap representation or "all occasions do
conspire against me," the effect is undefined. Either way,
the compiler is within its rights to eliminate the expression,
to eliminate p itself, and even to eliminate the call to
some_address() if doing so loses no side-effects.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Serve said:
what about in this case? Is the compiler allowed to not execute the
pointer dereference?

int main(void) {

char *p = some_address();


*p == 3;

return 0;

}

Yes, the compiler is allowed to omit the dereference and comparison
completely. The compiler is also allowed to assume that p points to an
object, at the same time even, and omit any (for example) null pointer
checks it finds afterwards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top