Undefined behavior in c/c++: i++ + ++i vs ++i + i++ [duplicate]

旧城冷巷雨未停 提交于 2019-12-18 09:45:25

问题


Imagine that we have the code below:

int i = 1;
int j = i++ + ++i;

I know that this is a Undefined Behavior, because before the semicolon, which is a sequence point, the value of i has been changed more than once. It means that the compiler may have two possibilities even if the precedence of operator plus is Left-to-Right:

case 1)

  1. take the value of i++ --- value of i is 1
  2. take the value of ++i --- value of i is 2
  3. do the operator plus and assign the result which is 3 to j and do the side effect of i++ (the order of this step is undefined too but we don't care because it won't change the result)

case 2)

  1. take the value of i++ --- value of i is 1
  2. do the side effect of i++ --- value of i is 2
  3. take the value of ++i --- current value of i is 3
  4. do the operator plus and assign the result which is 4 to j

If nothing is wrong here, I have a question:

int j = ++i + i++;

Is the code above still an Undefined Behavior?

In my opinion, there is only one possibility:

  1. do the side effect of ++i --- value of i is 2
  2. take the value of i++ --- value of i is 2
  3. do the operator plus and assign the result which is 4 to j and do the side effect of i++ (the order of this step is undefined too but we don't care because it won't change the result)

Am I right?

Btw I've read this link:
Undefined behavior and sequence points


回答1:


int j = ++i + i++;

is still undefined behavior since ++i and i++ can be processed simultaneously in multiple pipelines in some CPUs, which will lead to unpredictable results.




回答2:


No, you are not correct. Each of the expressions ++i and i++ modify i, so the statement modifies i more than once. That is undefined behaviour.

Note that more recent versions of the standards no longer include the concept of "sequence point".




回答3:


int i = 1;
int j = i++ + ++i;

Evaluation will happen from Right to left, i.e. firstly ++i will be processed then i++. So j will have value 4.

You will get a warning that "operation on 'i' may be undefined" but there is no problem with this expression.



来源:https://stackoverflow.com/questions/39900469/undefined-behavior-in-c-c-i-i-vs-i-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!