控制表达式评估 [英] controlling expressions evaluation

查看:86
本文介绍了控制表达式评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,

这里我提出的问题本身很简单,但是为了这个问题

我找不到答案。


C标准是否保证表达内部如


(x& y)


y不评估x是否为x。评估为0?


我们保证符合标准的编译器不能用于评估yb

第一个?


我的问题的原因是在我的代码中我必须用一个函数填充一个结构

(比如说my_table)(比如说)只有当my_table为NULL(即之前没有填充)时,才应调用fill_table()和fill_table




if这总是正确的吗?


if(!my_table&& fill_table())


谢谢。

-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp

Hi group,
here I come with a question which is quite simple per se, but for which
I can''t find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?

Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?

The reason for my question is that in my code I have to fill a structure
(say "my_table") with a function (say "fill_table()"), and fill_table
should be called only if my_table is NULL (i.e. wasn''t filled before).

if this always going to be correct?

if(!my_table && fill_table())

Thank you.
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

推荐答案

7月9日,下午5:04,Pietro Cerutti< g ... @ gahr.chwrote:
On Jul 9, 5:04 pm, Pietro Cerutti <g...@gahr.chwrote:

嗨组,

这里我带来了这个问题本身很简单,但是为了这个问题,我不能找到答案。


C标准是否保证在表达式中如此as


(x&& y)

y不评估x是否为x。评估为0?
Hi group,
here I come with a question which is quite simple per se, but for which
I can''t find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?



C标准在这方面保证从左到右的宣传。

C标准对此很清楚。

这里我引用6.5.13逻辑AND运算符:


语法

1 logical-AND-expression:

包容性或表达式

logical-AND-expression&&包容性或表达式

约束

2每个操作数都应具有标量类型。

语义

3和&&如果两个操作数都比较为
不等于0,则运算符应为1;否则,它是
产生0.结果类型为int。

4与按位二进制和&操作员,&&运算符保证

从左到右的评估;

在评估第一个操作数后有一个序列点。

如果是第一个操作数

比较等于0,第二个操作数未评估


我希望这完全可以回答你的疑问。

C standards gurantee left to right evlauation in this respect.
The C standards is clear about this.

Here I quote 6.5.13 Logical AND operator:

Syntax
1 logical-AND-expression:
inclusive-OR-expression
logical-AND-expression && inclusive-OR-expression
Constraints
2 Each of the operands shall have scalar type.
Semantics
3 The && operator shall yield 1 if both of its operands compare
unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation;
there is a sequence point after the evaluation of the first operand.
If the first operand
compares equal to 0, the second operand is not evaluated

I hope this should answer your doubt completely.


>

我们保证符合标准的编译器不能,无论用什么

目的,评估y第一?
>
Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?



根据标准,y不会被评估。但是,通过好像

规则,我认为编译器可能会这样做,如果它不影响代码的

行为。但最终代码产生的结果

应该是这样的,有从左到右的评价。


所以你可以很好地认为''x ''在''y'之前评估,如果它是在内部以这种方式完成的话,没有

打扰。

As per the standards, ''y'' won''t be evaluated first. But by "as if"
rule, I think a compiler might do that, if it doesn''t affect the
behavior of the code. But ultimately the result produced by the code
should be such that, there is left to right evaluation.

So you could very well think that ''x'' is evaluated before ''y'' without
bothering, if it is internally done that way or not.


>

我的问题的原因是,在我的代码中,我必须使用函数填充结构

(比如说my_table)(比如说fill_table() )和fill_table

只有在my_table为NULL时才会被调用(即之前没有填充)。


如果这总是正确的话?


if(!my_table&& fill_table())


谢谢。
>
The reason for my question is that in my code I have to fill a structure
(say "my_table") with a function (say "fill_table()"), and fill_table
should be called only if my_table is NULL (i.e. wasn''t filled before).

if this always going to be correct?

if(!my_table && fill_table())

Thank you.


Pietro Cerutti说:


< snip>
Pietro Cerutti said:

<snip>

我只是有点担心你的判决[...]在某些情况下

你可能会发现y的评估在评估开始之前

of x"。
I''m only a bit worried about your sentence "[...] in some situations
you might find that the evaluation of y begins before the evaluation
of x".



不要。 好像规则适用。

Don''t be. The "as if" rule applies.


几乎是什么意思?
What does it mean, practically?



实际上,这意味着你不需要有点担心。 :-)


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Practically, it means you don''t need to be a bit worried. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Pietro Cerutti写道:
Pietro Cerutti wrote:

Eric Sosman写道:
Eric Sosman wrote:

> Pietro Cerutti写道:
>Pietro Cerutti wrote:

>>嗨组,
我在这里提出一个问题本身很简单,但为此我找不到答案。

C标准是否保证在表达内部如

( x& y)

y不评估x是否为x。评估为0?
>>Hi group,
here I come with a question which is quite simple per se, but for which
I can''t find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?


是。

Yes.


>>我们是否保证符合标准的编译器不能用于任何目的,评估y第一?
>>Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?


是的。如果您研究生成的代码,您可能会怀疑这是是,因为在某些情况下你可能会发现y的评估是在x的评估完成之前开始的(想象x和y不是单个变量,而是更复杂的表达式)。但不要担心:结果
必须好像如果x为零,y'的评估严格晚于x',
或根本不会发生。在y的评估中可能发生的任何副作用或
错误都可能发生,直到x完成并发现为非零;如果x
结果为零,那么评估y的任何副作用或错误都不会发生。

Yes. If you study the generated code you might doubt
this "yes," because in some situations you might find that
the evaluation of y begins before the evaluation of x is
finished (imagine x and y not as single variables, but as
more complicated expressions). But don''t worry: the outcome
must be "as if" y''s evaluation is strictly later than x''s,
or doesn''t happen at all if x is zero. No side-effects or
errors that could occur in the evaluation of y can happen
until after x is finished and found to be non-zero; if x
turns out to be zero then no side-effects or errors from
evaluating y can occur at all.



感谢您的回答(也感谢CryptiqueGuy为您服务,

基本相同)。


我只是有点担心你的判决[...]在某些情况下你可能会发现y的评估在评估x之前开始;。

几乎是什么意思?


Thank you for your answer (and also thank you CryptiqueGuy for yours,
which is basically the same).

I''m only a bit worried about your sentence "[...] in some situations you
might find that the evaluation of y begins before the evaluation of x".
What does it mean, practically?



基本上,它意味着不要让生成的代码

混淆你。想象一下,这是一个多项表达式,

并且假设y的某些部分将被使用,无论是
如何测试结果:


if(x&& r + st)

printf(" Boo!\ n");

z = r + s + t;


这里,y是整个表达式`r + s t'',我们有这个表示只有当x为非时,才会评估
零。但编译器

可能会决定早期评估子表达式r + s,因为

它不会产生任何副作用(让我们假设)并且总和将无论如何都需要
。编译器可能会执行类似


tmp = r + s;

if(x&& tmp t)

printf(" Boo!\ n");

z = tmp + t;


....这就完全可以了。如果评估'r + s''会产生副作用

(例如,溢出生成信号),编译器可以

*而不执行此操作,因为价值

的x应该决定信号是出现在

输出之前还是之后,并且在重写的代码中它总是首先出现。

但是如果编译器能够弄清楚`r + s''没有副作用 -
效果需要担心,它可以在完成评估之前评估y

的那个组件x。


实际效果:从C程序员的角度来看,

none,因为程序运行好像 x == 0完全

抑制了y的评估。有人研究编译的

代码可能会因为看到y的某些部分提前评估而感到困惑,但这只是一种分心。我带来

的唯一原因就是回应你的短语出于任何目的。


-

Eric Sosman
es*****@ieee-dot-org.inva lid

Essentially, it means "Don''t let the generated code
confuse you." Imagine a y that is a multi-term expression,
and suppose that some parts of y will be used no matter
how the test comes out:

if (x && r + s t)
printf ("Boo!\n");
z = r + s + t;

Here, y is the entire expression `r + s t'', which we have
said will only be evaluated if x is non-zero. But the compiler
might decide to evaluate the sub-expression `r + s'' early, since
it will produce no side-effects (let''s assume) and the sum will
be needed anyhow. The compiler might do something like

tmp = r + s;
if (x && tmp t)
printf ("Boo!\n");
z = tmp + t;

.... and this would be perfectly all right. The compiler could
*not* do this if evaluating `r + s'' could produce a side-effect
(overflow generating a signal, for instance), because the value
of x should govern whether the signal occurs before or after the
output, and in the rewritten code it would always occur first.
But if the compiler can figure out that `r + s'' has no side-
effects to worry about, it can evaluate that component of y
before it finishes evaluating x.

Practical effects: From the C programmer''s perspective,
none, because the program operates "as if" x==0 completely
suppressed the evaluation of y. Someone studying the compiled
code might be confused by seeing some parts of y evaluated
early, but that''s just a distraction. The only reason I brought
it up was in response to your phrase "for whatever purpose."

--
Eric Sosman
es*****@ieee-dot-org.invalid


这篇关于控制表达式评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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