precedence question

D

davis_eric

Hi,

Just got a question about operator precedence...
supporse int a =5;
what the value of a++ * ++a should be?

A test run gave me 36, but I don't really know why... Could anyone
point me to how this expression is evaluated step by step?

Thanks a lot!
 
G

Gianni Mariani

Hi,

Just got a question about operator precedence...
supporse int a =5;
what the value of a++ * ++a should be?

A test run gave me 36, but I don't really know why... Could anyone
point me to how this expression is evaluated step by step?

It is undefined. The compiler is welcome to melt your CPU if you run
that code.

If you want a step by step answer, look at the generated code but it's
pointless since a conforming compiler may give you any answer it feels
like since it's not bound by the standard to do anything "defined".

I'm sure there's an FAQ entry on this one.
http://www.parashift.com/c++-faq-lite/
 
K

Kai-Uwe Bux

Hi,

Just got a question about operator precedence...
supporse int a =5;
what the value of a++ * ++a should be?

There is no "should" in this case: the code has undefined behavior because
it modifies the same variable twice between sequence points. If you search
for sequence points, you will find lots of information about this kind of
trap.
A test run gave me 36, but I don't really know why... Could anyone
point me to how this expression is evaluated step by step?

The standard does not prescribe how the expression is evaluated.


Best

Kai-Uwe Bux
 
T

terminator

There is no "should" in this case: the code has undefined behavior because
it modifies the same variable twice between sequence points. If you search
for sequence points, you will find lots of information about this kind of
trap.


The standard does not prescribe how the expression is evaluated.

Best

Kai-Uwe Bux

I think there are no more than two cases here:35(5*7) and 36(6*6) ,but
there is no garantee for either.

regards,
FM.
 
J

James Kanze

I think there are no more than two cases here:35(5*7) and 36(6*6) ,but
there is no garantee for either.

Just what part of "undefined" don't you understand? The
standard says quite clearly that modifying the same object twice
without an intervening sequence point is undefined behavior.
Anything the compiler does in such cases is correct. (Off hand,
I'd guess that 5*6 is the most likely result, but 6*7 wouldn't
surprise me either. But as far as the standard is concerned, 42
is just as correct as any other results, as is sending an
email to your boss, or deleting a random file from the disk.)
 
J

Jim Langston

I think there are no more than two cases here:35(5*7) and 36(6*6) ,but
there is no garantee for either.

Just what part of "undefined" don't you understand? The
standard says quite clearly that modifying the same object twice
without an intervening sequence point is undefined behavior.
Anything the compiler does in such cases is correct. (Off hand,
I'd guess that 5*6 is the most likely result, but 6*7 wouldn't
surprise me either. But as far as the standard is concerned, 42
is just as correct as any other results, as is sending an
email to your boss, or deleting a random file from the disk.)

================

It is quite obvious you don't understand what "undefined" means in this
context.
 
J

James Kanze

It is quite obvious you don't understand what "undefined" means in this
context.

I'm not quite sure who you mean by "you" in that sentence; from
the attributions, it would seem that you mean that I don't
understand what undefined means in the standard (which is the
context we are talking about), which is simply false. I've
worked too closely with the standards committee to not know what
was meant by something that fundamental in the standard. The
classical example of what is permitted by "undefined behavior"
(as in this case) is to make demons fly out of your nose (not
that I've ever seen a compiler with precisely that behavior).
Undefined means undefined. Or to quote the standard: "behavior,
such as might arise upon use of an erroneous program construct
or erroneous data, for which this International Standard imposes
NO requirements."
 
T

terminator

Just what part of "undefined" don't you understand? The
standard says quite clearly that modifying the same object twice
without an intervening sequence point is undefined behavior.
Anything the compiler does in such cases is correct. (Off hand,
I'd guess that 5*6 is the most likely result, but 6*7 wouldn't
surprise me either.
It is quite obvious you don't understand what "undefined" means in this
context

Actualy 25 or 42 or 49 or whatever else(although not as much the
prevoius ones) are likely to happen too. The compiler can peek either
of the cases ,hence this code is not what one can trust, that is why
the code is said to have undefined behavior .
But as far as the standard is concerned, 42
is just as correct as any other results, as is sending an
email to your boss, or deleting a random file from the disk.)

Nobody is that sick to write a compiler that does it, although that is
one implication of undefined behavoir.

BTW I tested:
int i=5;
cout << i++ << ' ' << ++i << endl;
i=5;
cout << i++*++ i << endl;
i=5;
cout << ++i << ' ' << i++ << endl;
i=5;
cout << ++i*i++ << endl;
and the output was:

6 7
36
6 5
36

because any combination of two numbers in the range 5..7 could be
taken.
 

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,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top