哪些问题你们在C和C ++,由于遇到的顺序点? [英] Which issues have you encountered due to sequence points in C and C++?

查看:96
本文介绍了哪些问题你们在C和C ++,由于遇到的顺序点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是导致不确定的行为,由于序列点规则两个常见问题:

  A [i] =我++; //具有读取和顺序点之间写
I = I ++; // 2序列点之间写道:

什么是你就遇到序列点其他的事情?

这实在是很难找到这些问题时,编译器不能够提醒我们。


解决方案

下面是编程原理和用c ++的做法由Bjarne Stroustup

一个简单的规则

  

如果你在一个前pression.Don't改变一个变量的值读取或
  在相同的前pression

写两次

  A [i] =我++; //我的值更改一次,而是两次读
I = I ++; //我的值更改一次,而是两次书面

Below are two common issues resulting in undefined behavior due to the sequence point rules:

a[i] = i++; //has a read and write between sequence points
i = i++;   //2 writes between sequence points

What are other things you have encountered with respect to sequence points?

It is really difficult to find out these issues when the compiler is not able to warn us.

解决方案

Here is a simple rule from Programming principles and practices using c++ by Bjarne Stroustup

"if you change the value of a variable in an expression.Don't read or write twice in the same expression"

a[i] = i++; //i's value is changed once but read twice
i = i++;   //i's value is changed once but written twice

这篇关于哪些问题你们在C和C ++,由于遇到的顺序点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆