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

风格不统一 提交于 2019-11-29 17:53:48
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.

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

AMS
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.

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