转让前pressions震荡 [英] assignment expressions and volatile

查看:105
本文介绍了转让前pressions震荡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有挥发物一般有一定的了解,但是有一个看似不起眼的情况下,在我不知道的事情应该如何按照工作标准。我读过C99的相关部分和SO一打或更多相关的帖子,但无法找到在这种情况下,逻辑,或在这种情况下,解释的地方。

I seem to have a reasonable understanding of volatiles in general, but there's one seemingly obscure case, in which I'm not sure how things are supposed to work per the standard. I've read the relevant parts of C99 and a dozen or more related posts on SO, but can't find the logic in this case or a place where this case is explained.

假设我们有这一块code的:

Suppose we have this piece of code:

  int a, c;
  volatile int b;
  a = b = 1;
  c = b += 1; /* or equivalently c = ++b; */

应该 A 这样的评价:

  b = 1;
  a = b; // volatile is read

或者是这样的:

  b = 1;
  a = 1; // volatile isn't read

同样,应 C 是这样的评价:

Similarly, should c be evaluated like this:

  int tmp = b;
  tmp++;
  b = tmp;
  c = b; // volatile is read

或者是这样的:

  int tmp = b;
  tmp++;
  b = tmp;
  c = tmp; // volatile isn't read

在简单的情况下,像 A = B; C = B:事情是清楚的。但如何对上面的人?

In simple cases like a = b; c = b; things are clear. But how about the ones above?

基本上,问题是,究竟是什么前pression有分配后左操作数的值意味着C99的6.5.16c3当对象是挥发性的:

Basically, the question is, what exactly does "expression has the value of the left operand after the assignment" mean in 6.5.16c3 of C99 when the object is volatile?:

赋值运算符存储由指定的对象的值
      左操作数。赋值前pression有左操作数的值
      在转让之后
,但不是左值。

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

这是否意味挥发额外的读取产生转让前pression的价值?

Does it imply an extra read of the volatile to produce the value of the assignment expression?

更新

所以,这里的窘境。

如果不是从挥发物的额外读得到后的分配对象的价值,那么编译器可以假设挥发物 B

If "the value of the object after the assignment" is not obtained from the extra read of the volatile object, then the compiler makes the assumption that the volatile object b:


  • 能容纳的被写进它任意 INT 价值,它可能不是(比如,位0是硬连接至0,这是不寻常与硬件寄存器,为此,我们应该使用挥发物)的事情

  • 当获得前pression值(并再次它可以与硬件寄存器中的问题)当发生执行分配的写入点和点之间不能更改

  • is capable of holding an arbitrary int value that gets written into it, which it may not be (say, bit 0 is hardwired to 0, which is not an unusual thing with hardware registers, for which we are supposed to use volatiles)
  • cannot change between the point when the assigning write has occurred and the point when the expression value is obtained (and again it can be a problem with hardware registers)

的,由于所有的是,前pression值,如果不是从易失性对象的额外读出得到的,不会产生挥发性对象,它的标准的权利要求应该是这样的值。

And because of all that, the expression value, if not obtained from the extra read of the volatile object, does not yield the value of the volatile object, which the standard claims should be the case.

这两种假设似乎不挥发物的性质,以适应井

Both of these assumptions don't seem to fit well with the nature of volatile objects.

如果,OTOH,从额外的隐含读获得了分配后的对象的值,所述挥发性对象,然后用挥发性左操作数评估分配前pressions的副作用取决于是否除权pression值或不使用或完全是任意的,这将是一个奇怪的,意外的,记录不行为。

If, OTOH, "the value of the object after the assignment" is obtained from the extra implied read of said volatile object, then the side effects of evaluating assignment expressions with volatile left operands depend on whether the expression value is used or not or are completely arbitrary, which would be an odd, unexpected and poorly documented behavior.

推荐答案

C11澄清,这是不确定的。

C11 clarifies that this is undefined.

您可以找到的C11 这里的最终草案。你现在所引用的第二句是指脚注111:

You can find the final draft of C11 here. The second sentence you quoted now refers to footnote 111:

赋值运算符存储在由左操作数所指定的对象的值。转让前pression有左操作数的分配后的值, 111),但不是左值。

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,111) but is not an lvalue.

脚注111这样说:

111)的执行被允许读取对象,以确定该值,但不要求,即使当对象具有挥发性限定类型

111) The implementation is permitted to read the object to determine the value but is not required to, even when the object has volatile-qualified type.

这篇关于转让前pressions震荡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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