执行顺序 [英] Order of execution

查看:71
本文介绍了执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

最近的帖子之一给宏做交换


#define SWAP(m,n)(tmp =(m ),(m)=(n),(n)= tmp)

如果执行从左到右,此宏将起作用。


即步骤1)tmp = m

步骤2)m = n

步骤3)n = tmp


但是我希望执行的顺序由编译器决定。

C规范是否说出关于这个exectuion顺序的任何事情?


这个宏会不会在所有编译器上工作?


Plz分享您的想法...


谢谢

Sabi

Hi all,
one of the recent post gives the macro to do swap

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

This macro will work, if the execution is from left to right.

That is step 1) tmp=m
step 2) m=n
step 3) n=tmp

But I hope order of execution is decided by the compiler.
Does C specification is saying any thing about this order of exectuion?

Will this macro work on all compilers?

Plz share your thoughts...

Thanks
Sabi

推荐答案

" Sabiyur" <萨*********** @ gmail.com>写道:
"Sabiyur" <Sa***********@gmail.com> wrote:
大家好,
最近的帖子之一让宏做交换

#define SWAP(m,n)(tmp =(m) ,(m)=(n),(n)= tmp)

如果执行是从左到右,这个宏将起作用。

这是步骤1) tmp = m
步骤2)m = n
步骤3)n = tmp

但我希望执行顺序由编译器决定。
C规范关于这个exectuion顺序是什么意思?

这个宏会在所有编译器上工作吗?
Hi all,
one of the recent post gives the macro to do swap

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

This macro will work, if the execution is from left to right.

That is step 1) tmp=m
step 2) m=n
step 3) n=tmp

But I hope order of execution is decided by the compiler.
Does C specification is saying any thing about this order of exectuion?

Will this macro work on all compilers?



是的,它适用于所有C编译器。

宏中使用的逗号运算符保证了从左到右的评估。


(当然,必须首先定义tmp。)


Yes, it will work on all C compilers. The comma operator used in the
macro guarantees left-to-right evaluation.

(Of course, tmp must be defined first.)


" Sabiyur" <萨*********** @ gmail.com>写道:
"Sabiyur" <Sa***********@gmail.com> wrote:
最近的帖子之一给宏做交换

#define SWAP(m,n)(tmp =(m),(m )=(n),(n)= tmp)

如果执行从左到右,则此宏将起作用。


不,它不会;它会工作,_if_(如果这个更大,那么)tmp与m和n相同的类型是b $ b,或者是兼容的类型。

那是第1步)tmp = m
步骤2)m = n
步骤3)n = tmp

但我希望执行顺序由编译器决定。
C是否规范是关于这个exectuion顺序的任何事情?
one of the recent post gives the macro to do swap

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

This macro will work, if the execution is from left to right.
No, it won''t; it will work, _if_ (and that''s a much bigger if) tmp has
the same type as m and n, or a compatible one.
That is step 1) tmp=m
step 2) m=n
step 3) n=tmp

But I hope order of execution is decided by the compiler.
Does C specification is saying any thing about this order of exectuion?




是的。不一般,但是逗号上有一个序列点。这个

意味着无论在帽子下发生了什么诡计,程序很多都会像从左到右一样执行。

但是,使用这个宏意味着你必须声明第三个对象;

这个对象_must_被称为tmp;在调用此宏时,它不能在

中保持重要值;并且它必须被声明为具有与m和n兼容的类型

(因此不使用它来交换长的

加倍一行,并构造下一行对于一般性来说太多了。


所有这些意味着使用这个宏可能比每次需要时写出这条线更麻烦。 。至少这意味着

你可以交换几种类型的价值,而不必诉诸于黑客




{

the_appropriate_type tmp;

SWAP(object_1,object_2_;




(哦,和当然,通过放弃宏,你也可以交换对象,这些对象本身叫做tmp,但这是一个较小的优势。)


Richard



Yes. Not generally, but there''s a sequence point at the commas. This
means that whatever trickery happens under the bonnet, the program much
at least behave as if it was executed left-to-right.

However, using this macro means that you have to declare a third object;
this object _must_ be called tmp; it must not hold an important value at
the moment this macro is called; and it must be declared to have a type
that is compatible with both m and n (so no using it to swap long
doubles one line, and structs the next line. So much for generality).

All this means that using this macro is probably rather more bother than
just writing this line out every time you need it. At least that means
you can swap several types of value, without having to resort to hacks
such as

{
the_appropriate_type tmp;
SWAP(object_1, object_2_;
}

(Oh, and of course by forgoing the macro you can also swap objects which
are themselves called tmp, but that''s a lesser advantage.)

Richard


Sabiyur schrieb:
Sabiyur schrieb:
大家好,
最近的帖子之一让宏做交换

#define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp)

如果执行从左到右,则此宏将起作用。

这是步骤1)tmp = m
步骤2)m = n
步骤3)n = tmp

但我希望执行顺序是由编译器决定。
C规范是否对这个exectuion顺序说了什么?

这个宏会在所有编译器上工作吗?
Hi all,
one of the recent post gives the macro to do swap

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

This macro will work, if the execution is from left to right.

That is step 1) tmp=m
step 2) m=n
step 3) n=tmp

But I hope order of execution is decided by the compiler.
Does C specification is saying any thing about this order of exectuion?

Will this macro work on all compilers?




C有一个叫做序列点的概念。

在两个连续的序列点之间,执行顺序

不是由C标准确定 - 它只需要

语义正确。

但是,你可以确定序列点之前的代码

在序列点之后进行评估(或者你的编译器/
平台表现得好像这样)。

逗号运算符为你提供了这样一个序列点,即

预处理版的

tmp =(m)

将在预处理版本的

之前执行(m)=(n),(n)= tmp

并再次应用此规则,

(m)=(n)

之前执行
$ b(n)= tmp


阅读常见问题解答中关于序列点的更多信息:
http://c-faq.com/expr/seqpoints.html

和剩下的第3章。


干杯

Michael

-

电子邮件:我的是一个/ at / gmx / dot / de address。



C has a concept called "sequence points".
Between two consecutive sequence points, the order of execution
is not determined by the C standard -- it only has to be
semantically correct.
However, you can be sure that the code before the sequence point
is evaluated after the sequence point (or that your compiler /
platform behaves as if this is the case).
The comma operator gives you such a sequence point, i.e.
the preprocessed version of
tmp = (m)
will be executed before the preprocessed version of
(m) = (n), (n) = tmp
and, applying this rule once more,
(m) = (n)
is executed before
(n) = tmp

Read more on sequence points in the FAQ:
http://c-faq.com/expr/seqpoints.html
and the rest of chapter 3.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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