setjmp / longjmp好吗? [英] Is setjmp/longjmp ok?

查看:54
本文介绍了setjmp / longjmp好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setjmp / longjmp真的应该用在快速平凡的ANSI C中吗?

代码?或者像goto一样皱起眉头?我有需要,但我不想要b $ b使用价格昂贵的东西,不能得到一致支持,或者某些东西

可能会引入异国情调的文本片段等等。


具体来说,我有一个毛茸茸的算法循环,它使用的是当前

a宏V.这里是一个snipplet:


for(k = d; k> = -d; k - = 2){

if(k == -d ||(k!= d&&) ; V(fwd,m,k - 1)< V(fwd,m,k + 1))){

x = V(fwd,m,k + 1);

}否则{

x = V(fwd,m,k - 1)+ 1;

}

y = x - k ;


但是现在我必须用一个函数(比如vfn)替换V宏,它将需要指示发生了错误。问题是我;


A)用一个返回-1的函数替换每个宏V以表示发生了

错误并检查每个宏如下所示:


for(k = d; k> = -d; k - = 2){

int v1 = vfn(fwd,m ,k - 1);

int v2 = vfn(fwd,m,k + 1);

if(v1 == -1 || v2 == -1 ){

返回-1;

}

if(k == -d ||(k!= d&& v1) < v2)){

if((x = vfn(fwd,m,k + 1))== -1){

return -1;

}

... yuk - 和vfn被调用无论

是否k == -d或k == d




B)在发生错误时使用longjmp干净地返回,无论

毛状循环的状态如何:


if(setjmp(env)== 1){

返回-1;

}

for(k = d; k> = -d; k - = 2){

if(k == -d ||(k!= d&& vfn) fwd,m,k - 1,& env)< vfn(fwd,m,k + 1,& e nv))){

x = vfn(fwd,m,k + 1,& env);

}否则{

.. 。


当错误发生时,vfn调用longjmp,如:


longjmp(* env,1);

谢谢,

Mike

Should setjmp/longjmp really be used in a fast mundane ANSI C piece of
code? Or is it frowned apon like goto? I have a need but I don''t want to
use something that is costly, isn''t supported consistenly, or something
that might pull in exotic text segments, etc.

Specifically I have a hairly algorithm loop that uses what is currently
a macro V. Here''s a snipplet:

for (k = d; k >= -d; k -= 2) {
if (k == -d || (k != d && V(fwd, m, k - 1) < V(fwd, m, k + 1))) {
x = V(fwd, m, k + 1);
} else {
x = V(fwd, m, k - 1) + 1;
}
y = x - k;

But now I must replace the V macro with a function (say vfn) that will
need to indicate an error has occured. The question is do I;

A) replace each macro V with a function that returns -1 to indicate an
error has occured and check it with each call like:

for (k = d; k >= -d; k -= 2) {
int v1 = vfn(fwd, m, k - 1);
int v2 = vfn(fwd, m, k + 1);
if (v1 == -1 || v2 == -1) {
return -1;
}
if (k == -d || (k != d && v1 < v2)) {
if ((x = vfn(fwd, m, k + 1)) == -1) {
return -1;
}
... yuk - and vfn is called regardless of
whether or not k == -d or k == d

or

B) use longjmp when the error occurs to cleanly return regardless of
the state of the hairy loop like:

if (setjmp(env) == 1) {
return -1;
}
for (k = d; k >= -d; k -= 2) {
if (k == -d || (k != d && vfn(fwd, m, k - 1, &env) < vfn(fwd, m, k + 1, &env))) {
x = vfn(fwd, m, k + 1, &env);
} else {
...

where vfn calls longjmp when the error occurs like:

longjmp(*env, 1);

Thanks,
Mike

推荐答案

Michael B Allen写道:
Michael B Allen wrote:
setjmp / longjmp真的应该用在快速普通的ANSI C中吗?或者像goto一样皱起眉头?我有需要,但我不想使用昂贵的东西,不能保持一致,或者可能会引入异国情调的文本片段等等。
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of
code? Or is it frowned apon like goto? I have a need but I don''t want to
use something that is costly, isn''t supported consistenly, or something
that might pull in exotic text segments, etc.

<很多平台都支持
setjmp / longjmp,所以我不会担心它的不可用性。

最常用的是信号处理程序,例如信号(SIGINT,

your_function_here),返回main()或事件循环。

虽然你的使用并不是特别邪恶,如果你能干净地解开堆栈,你为什么要用非本地的getos来复杂你的代码?b $ b? longjmp

可能会让你快点回来,但YMMV。



setjmp/longjmp tends to be supported across a lot of platforms, so I
wouldn''t worry about its unavailability.

The most frequent usage is in signal handlers, e.g., signal(SIGINT,
your_function_here), to return back to main() or an event loop.

While your usage isn''t particularly evil, why would you want to complicate
your code with non-local gotos if you can unwind the stack cleanly? longjmp
may return you back a little faster, but YMMV.


Michael B Allen写道:
Michael B Allen wrote:
setjmp / longjmp真的应该用在快速平凡的ANSI C中吗?或者像goto一样皱起眉头?我有需要,但我不想使用昂贵的东西,不能保持一致,或者可能会引入异国情调的文本片段等等。
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of
code? Or is it frowned apon like goto? I have a need but I don''t want to
use something that is costly, isn''t supported consistenly, or something
that might pull in exotic text segments, etc.

<很多平台都支持
setjmp / longjmp,所以我不会担心它的不可用性。

最常用的是信号处理程序,例如信号(SIGINT,

your_function_here),返回main()或事件循环。

虽然你的使用并不是特别邪恶,如果你能干净地解开堆栈,你为什么要用非本地的getos来复杂你的代码?b $ b? longjmp

可能会让你快点回来,但YMMV。



setjmp/longjmp tends to be supported across a lot of platforms, so I
wouldn''t worry about its unavailability.

The most frequent usage is in signal handlers, e.g., signal(SIGINT,
your_function_here), to return back to main() or an event loop.

While your usage isn''t particularly evil, why would you want to complicate
your code with non-local gotos if you can unwind the stack cleanly? longjmp
may return you back a little faster, but YMMV.


2004年4月30日星期五20:51:10 -0700, - wombat-< sc **** @ cs.ucla.edu>

在comp.lang.c中写道:
On Fri, 30 Apr 2004 20:51:10 -0700, -wombat- <sc****@cs.ucla.edu>
wrote in comp.lang.c:
Michael B Allen写道:
Michael B Allen wrote:
setjmp / longjmp真的应该用在快速平凡的ANSI C中吗?或者像goto一样皱起眉头?我有需要,但我不想使用昂贵的东西,不能保持一致,或者可能会引入异国文本片段的东西等等。
setjmp / longjmp往往支持很多平台,所以我不担心它不可用。
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of
code? Or is it frowned apon like goto? I have a need but I don''t want to
use something that is costly, isn''t supported consistenly, or something
that might pull in exotic text segments, etc.
setjmp/longjmp tends to be supported across a lot of platforms, so I
wouldn''t worry about its unavailability.




托管环境的所有C实现支持setjmp和

longjmp。在托管环境中,任何没有的东西都不是C

实现,无论相反的声明如何。

最常用的是信号处理程序,例如信号(SIGINT,
your_function_here),返回main()或事件循环。


在异步调用的信号处理程序中调用longjmp,

不是通过调用raise()或abort(),产生未定义的

行为。

虽然你的使用并不是特别邪恶,但如果你可以放松堆栈,你为什么要用非本地的getos复杂你的代码?干净? longjmp
可能会让你回来快一点,但YMMV。



All implementations of C for hosted environments support setjmp and
longjmp. Anything that does not, in a hosted environment, is not a C
implementation regardless of claims to the contrary.
The most frequent usage is in signal handlers, e.g., signal(SIGINT,
your_function_here), to return back to main() or an event loop.
Calling longjmp in a signal handler that was invoked asynchronously,
that is other than by a call to raise() or abort(), produces undefined
behavior.
While your usage isn''t particularly evil, why would you want to complicate
your code with non-local gotos if you can unwind the stack cleanly? longjmp
may return you back a little faster, but YMMV.




一般来说,需要在普通程序中使用longjmp

普通错误处理表明需要更好的设计。他们

当然是例外,但他们非常非常少。


-

Jack Klein
主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Generally speaking, the need to use longjmp in an ordinary program for
ordinary error handling indicates a need for a better design. They
are exceptions, of course, but they are very, very few.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于setjmp / longjmp好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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