undefined behaviour

D

Dan Cernat

Someone posted earlier an example that looked like taken from the
standard. It was: i = 7, i++, i++; and the result should be 9 (i = 9
in the end)
I thought that the compiler isn't required to do the assignement until
the right hand side expression is fully evaluated. Apparently, the
above expression is handled as:
i = 7;
i++;
i++;
which I don't think is right. Could someone explain, please?

/dan
 
J

Josh Sebastian

Someone posted earlier an example that looked like taken from the
standard. It was: i = 7, i++, i++; and the result should be 9 (i = 9
in the end)
I thought that the compiler isn't required to do the assignement until
the right hand side expression is fully evaluated. Apparently, the
above expression is handled as:
i = 7;
i++;
i++;
which I don't think is right. Could someone explain, please?

Yes, it's handled like that. You're almost right about assignment:
operator= has higher precedence than operator, so it's the same as if you
wrote

(i=7),i++,i++

The assignment happens first, followed by a post-increment, followed by a
post-increment. The comma operator inserts a sequence point, so the
behavior isn't undefined.

Josh
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top