免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1629 | 回复: 6
打印 上一主题 下一主题

i = i++ + 1 ! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-01 12:35 |只看该作者 |倒序浏览

  1. #include <stdio.h>;
  2. #include <stdlib.h>;

  3. int main(void)
  4. {
  5.         int     n, i = 0;
  6.         int     i = 1;
  7.         i = i++ + 1;
  8.         printf("%d\n", i);

  9.         return 1;
  10. }
复制代码


请解释运算过程!

论坛徽章:
0
2 [报告]
发表于 2003-03-01 15:59 |只看该作者

i = i++ + 1 !


  1. #include <stdio.h>;
  2. #include <stdlib.h>;

  3. int main(void)
  4. {
  5.         int     i = 1;

  6.         // i = i++ + 1;
  7.         // equal to
  8.         i = i+1;
  9.         i++;

  10.         //but, look this ,what's running order?
  11.         i = i++ + ++i;
  12.         //equal to
  13.         ++i;
  14.         i=i+i;
  15.         ++i;
  16.         //you can try it .

  17.         printf("%d\n", i);

  18.         return 1;
  19. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-03-01 16:28 |只看该作者

i = i++ + 1 !

研究一下编译原理,类似问题就全明白了。

论坛徽章:
0
4 [报告]
发表于 2003-03-02 10:58 |只看该作者

i = i++ + 1 !

需要高手解释一下啊~~~~~~

试出来的有时候不一定正确的,在别的论坛看见过,他们说是 "undefined behaviour" 我也不太明白

论坛徽章:
0
5 [报告]
发表于 2003-03-02 11:12 |只看该作者

i = i++ + 1 !

别的论坛的,关于k = ++i + i++ * ++i; 的~~~~~~~

XX wrote:
>; i think the statement "k = ++i + i++ * ++i;" can be divide into several
>; steps :    ++i;
>;            ++i;
>;            k=i+i*i;
>;            i++;

First of all, this is the third time you've sent this message, I don't
really see what the point of repeating it so many times is.

Secondly, you cannot divide this expression into several steps. Your
code above assumes a certain ordering of evaluation for the
operations, and as I explained before, there is no such ordering. The
compiler is free to evaluate this expression however it wants. That is
what "undefined behavior" means.

The original statement results in undefined behavior. Your multi-step
process does not, however there is nothing that even remotely suggests
that the compiler will evaluate that expression in the manner you have
laid out.

The original expression has no sequence points. A sequence point is a
point in the execution of the code where all previous side effects
have completed, and no further side effects have begun computing. In
the original expression, there are no sequence points between the
multiple modifications of the variable i. This situation always
results in undefined behavior, and it is explicitly stated so in the
C++ standard.

Your multi-step expression has a sequence point in between each
portion of the expression, namely the end of the statement with a
semicolon. This provides a point for the compiler to ensure that all
side effects are complete before beginning the next operation.
For expressions like i++, the compiler can do this by storing a
temporary value, adding 1 to i, then using the stored value in the
resulting expression, or it could use i's value directly, and then
increment afterward. The sequence point gives the compiler a point by
which the side effect has to be completed before it can execute
further.

Without the sequence point, the compiler's execution of ++i and i++
could get severly botched, since it might be overwriting values it was
going to use to achieve the side effect or the expression's value.

This is a very important point to remember:

NEVER use multiple operations that have side effects on the same
object without a sequence point in between.

Doing so results in undefined behavior. The original expression above
is one example. Calling a function like foo(i++, i++) is another.
These are all bad practice and should be avoided like the plague.

Hope this helps clarify some of the confusion.

论坛徽章:
0
6 [报告]
发表于 2003-03-02 11:23 |只看该作者

i = i++ + 1 !

这种++确实是有问题

所以一个表达式中不应该多用

以前是有人问我
int i=5,sum;
sum=++i*++i*++i

得到的结果很奇怪

所以
大家写代码时这种代码最好不要写
减少出错性

论坛徽章:
0
7 [报告]
发表于 2003-03-02 12:00 |只看该作者

i = i++ + 1 !

其实我并不赞同这种写法!
不过了解一下也是可以的!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP