多项任务评估辩论 [英] Multiple Assignment Evaluation Debate
问题描述
大家好,
一位同事和我一直在争论
对a = b = c这句话的正确预期。两个不同版本的GCC
最终将其编译为b = c; a = b而另一个结束了
将其编译为a = c; b = c。 (两者均未启用优化)。
我们如何注意到您可能会问?好吧,在我们的例子中''b''是一个内存
映射寄存器,它只有一个可选择的可写位数。他/ b $ b声称它是一个'C标准',它将被评估为a = c; b
= c。我个人认为将b = c评估为b $ b更有意义。 a = b,虽然我永远不会编写具有
可疑操作的代码。任何人都可以解决这个争论吗?
Hi All,
A coworker and I have been debating the ''correct'' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other ended up
compiling it as a = c; b = c. (Both with no optimizations enabled).
How did we notice you may ask? Well, in our case ''b'' was a memory
mapped register that only has a select number of writable bits. He
claims it has been a ''C Standard'' that it will be evaluted as a = c; b
= c. I personally believe that it would make more sense for it to be
evaluated as b = c; a = b, although I would never write code that has a
questionable operation. Can anyone settle this debate?
推荐答案
be * **@uwalumni.com 说:
大家好,
一位同事和我一直在争论'正确'的期望
评估短语a = b = c。两个不同版本的GCC
最终将其编译为b = c; a = b而另一个最终将其编译为a = c; b = c。 (两者均未启用优化)。
我们如何注意到您可能会问?好吧,在我们的例子中,''b''是一个内存
映射寄存器,只有一个可选择的可写位数。他声称它是一个'C标准',它将被评估为a = c; b
= c。我个人认为将它评估为b = c会更有意义。 a = b,虽然我永远不会编写具有可疑操作的代码。任何人都可以解决这个争论吗?
Hi All,
A coworker and I have been debating the ''correct'' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other ended up
compiling it as a = c; b = c. (Both with no optimizations enabled).
How did we notice you may ask? Well, in our case ''b'' was a memory
mapped register that only has a select number of writable bits. He
claims it has been a ''C Standard'' that it will be evaluted as a = c; b
= c. I personally believe that it would make more sense for it to be
evaluated as b = c; a = b, although I would never write code that has a
questionable operation. Can anyone settle this debate?
结合性是:a =(b = c);但a,b和
c的评估顺序未指定。标准只要求a和b
的新值在前一个时间之后的某个时间提供,并且在下一个之后,
序列点。
因此,gcc完全有权将其编译为:a = c; b = c;或b = c;
a = c;或b = c; a = b;或tmp = c; a = tmp; b = tmp;或任何其他
组合给出相同的结果。
如果你依赖于评估的顺序,a,b和c的任何副作用< b / b
最终可能会咬你。
-
Richard Heathfield
" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
The associativity is: a = (b = c); but the order of evaluation of a, b, and
c is unspecified. The Standard only requires that the new values of a and b
are provided at some time after the previous, and before the following,
sequence point.
So gcc is quite within its rights to compile it to: a = c; b = c; or b = c;
a = c; or b = c; a = b; or tmp = c; a = tmp; b = tmp; or any other
combination that gives the same result.
If you depend on the order of evaluation, any side effects of a, b, and c
are likely to bite you eventually.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
在2006-03-10, be *** @ uwalumni.com < be *** @ uwalumni.com>写道:
On 2006-03-10, be***@uwalumni.com <be***@uwalumni.com> wrote:
大家好,
一位同事和我一直在争论评估a = b = c这句话的'正确'期望。两个不同版本的GCC
最终将其编译为b = c; a = b而另一个最终将其编译为a = c; b = c。 (两者均未启用优化)。
我们如何注意到您可能会问?好吧,在我们的例子中,''b''是一个内存
映射寄存器,只有一个可选择的可写位数。
如果你那样做,那就是挥发性。关键字可以做一些事情
的可预测性
他声称它是一个'C标准',它将被评估为a =
c; b = c。
他对标准的解释与我的相同。
我个人认为将其评估为更有意义b = c; a = b,虽然我永远不会编写具有可疑操作的代码。谁能解决这个争论?
Hi All,
A coworker and I have been debating the ''correct'' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other ended up
compiling it as a = c; b = c. (Both with no optimizations enabled).
How did we notice you may ask? Well, in our case ''b'' was a memory
mapped register that only has a select number of writable bits.
If you do that, a sprinkling of the "volatile" keyword may do something
for predictability
He claims it has been a ''C Standard'' that it will be evaluted as a =
c; b = c.
His interpretaion of the standard is the same as mine.
I personally believe that it would make more sense for it to be
evaluated as b = c; a = b, although I would never write code that has a
questionable operation. Can anyone settle this debate?
使用volatile。你可能仍然会得到错误的答案,但它更可能是一致的
。
Use volatile. You might still get the wrong answer, but it''s more likely
to be consistent.
Richard Heathfield写道:
Richard Heathfield wrote:
be***@uwalumni.com 说:
大家好,
一位同事和我一直在讨论对a = b = c这个短语进行评估的正确期望。两个不同版本的GCC
最终将其编译为b = c; a = b而另一个最终将其编译为a = c; b = c。 (两者均未启用优化)。
我们如何注意到您可能会问?好吧,在我们的例子中,''b''是一个内存
映射寄存器,只有一个可选择的可写位数。他声称它是一个'C标准',它将被评估为a = c; b
= c。我个人认为将它评估为b = c会更有意义。 a = b,虽然我永远不会编写具有可疑操作的代码。谁能解决这个争论?
Hi All,
A coworker and I have been debating the ''correct'' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other ended up
compiling it as a = c; b = c. (Both with no optimizations enabled).
How did we notice you may ask? Well, in our case ''b'' was a memory
mapped register that only has a select number of writable bits. He
claims it has been a ''C Standard'' that it will be evaluted as a = c; b
= c. I personally believe that it would make more sense for it to be
evaluated as b = c; a = b, although I would never write code that has a
questionable operation. Can anyone settle this debate?
结合性是:a =(b = c);但是a,b和
c的评估顺序是未指定的。标准只要求a和b
的新值在前一个之后,之后的序列点之前的某个时间提供。
所以gcc是相当的它有权将其编译为:a = c; b = c;或b = c;
a = c;或b = c; a = b;或tmp = c; a = tmp; b = tmp;或任何其他
组合产生相同的结果。
如果你依赖于评估的顺序,a,b和c
的任何副作用可能会咬你最终。
The associativity is: a = (b = c); but the order of evaluation of a, b, and
c is unspecified. The Standard only requires that the new values of a and b
are provided at some time after the previous, and before the following,
sequence point.
So gcc is quite within its rights to compile it to: a = c; b = c; or b = c;
a = c; or b = c; a = b; or tmp = c; a = tmp; b = tmp; or any other
combination that gives the same result.
If you depend on the order of evaluation, any side effects of a, b, and c
are likely to bite you eventually.
你错过了他的问题。无论与评估订单有什么关系,b的中间分配都可以影响分配给a的值。 int = unsigned char = int,
例如。如果b是易失性的,也可能会改变图片,
正如另一个线程所讨论的那样。假设a = b = c可以被b = c,a = b
$ b $取代,这是不安全的(因为
可以辩论) b即使b是挥发性的。但是说a = b = c意味着(无论是什么
顺序)a = c和b = c是完全错误的。
You missed the point of his question. Regardless of anything having
to do with evaluation order, the intermediate assignment to b can
affect the value that is assigned to a. int = unsigned char = int,
for example. If b is volatile that may also change the picture,
as another thread discusses. It isn''t safe to assume (because
it''s open to debate) that a = b = c may be replaced by b = c, a = b
even if b is volatile. But saying a = b = c means (in whatever
order) a = c and b = c is simply wrong.
这篇关于多项任务评估辩论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!