longjmp vs goto [英] longjmp vs goto

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

问题描述

有人可以澄清跳远和

gotos之间的区别吗?为什么一个人皱眉而不是另一个?在使用longjmp变得不可避免的任何情况下,真的有吗?一个真实的

代码片段肯定会有所帮助。


问候,

Nimmi

解决方案

Nimmi Srivastav写道:

有人可以澄清跳远和
之间的区别吗?


goto构造是一种很难读取函数的好方法,而

longjmp函数是让它难以读取的好方法一个完整的

计划。

为什么一个人不赞成而不是另一个?


因为当我对另一个人皱眉时你没有看。

是否真的存在使用longjmp变得不可避免的任何情况?


不在我的手表上。

一个真实的代码片段肯定会有所帮助。




帮忙什么?我已经/有头疼了。

好​​吧,让我们试着聪明地回答这个问题。首先,它们之间的基本区别是,longjmp允许你在

函数之间跳转 - 或者更确切地说,setjmp让你说这里!和longjmp然后

稍后传送给你。


根据我的经验,通常可以而且明智地管理你没有

要么。我认为它们都可以*明智地使用,但是我觉得它没有它们就很容易做到。


毫无疑问会有一些这里的人对此有不同的看法。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Nimmi Srivastav写道:

有人可以澄清跳远和跳远之间的区别吗?


它们确实*不具有可比性。

为什么一个人皱眉而不是另一个?


setjmp / longjpm函数对于新手或业余程序员来说都不是*。

是否有任何情况
使用longjmp变得不可避免?


setjmp / longjmp机制

是C程序员抛出的方式例外。

不幸的是,它确实*不会*清理后。

setjmp后从免费存储中分配的内存

将不会免费'd。

一个真实的代码片段肯定会有所帮助。




看一下手册页:

http:// www.rahul.net/cgi-bin/userbin...gjmp&section=3

以下代码片段表示
$的控制流程b $ b setjmp()和longjmp()组合:


/函数声明/

...

jmp_buf my_environment ;

...

if(setjmp(my_environment)){

/ *寄存器变量具有不可预测的值* /
$从longjmp返回后的b $ b代码

...

}否则{

/ *不修改此段中的寄存器变量代码* /

这是setjmp
...

}


ni ************* @ yahoo.com (Nimmi Srivastav)写道:

#有人可以澄清跳远和

#gotos之间的区别吗?为什么一个人皱眉而不是另一个?真的是

#使用longjmp变得不可避免的任何情况?一个真实的

#代码片段肯定会有所帮助。


(1)你不能从一个功能转到另一个功能。您可以从协议栈中的任何位置向上调整

函数。


(2)ANS C没有标签变量,但是jmp_bufs是变量。


在我的递归上升解析器中,错误恢复在恢复之前放弃当前解析

并从初始状态重新启动(或多或少)。 />
而不是在解析器中有初始函数检查返回如果发生

解析错误,我只是将longjmp从错误检测发送到错误

恢复。


-

Derk Gwen http://derkgwen.250free.com/html/index.html

我今天甚至不应该在这里。

Can someone kindly clarify the distinction between long jumps and
gotos? Why is one frowned upon and not the other? Is there really
any situation where use of longjmp becomes inevitable? A real-life
code snippet would surely help.

Regards,
Nimmi

解决方案

Nimmi Srivastav wrote:

Can someone kindly clarify the distinction between long jumps and
gotos?
The goto construct is a good way to make it hard to read a function, whereas
the longjmp function is a good way to make it hard to read an entire
program.
Why is one frowned upon and not the other?
Because you weren''t looking whilst I was frowning at the other.
Is there really
any situation where use of longjmp becomes inevitable?
Not on my watch.
A real-life code snippet would surely help.



Help what? I already /have/ a headache.
All right, let''s try to answer this intelligently. Firstly, the basic
difference between them is that longjmp lets you hop around between
functions - or rather, setjmp lets you say "HERE!" and longjmp then
teleports you there later on.

In my experience, it is generally possible, and wise, to manage without
either. Both of them *can* be used wisely, I suppose, but I find it
remarkably easy to do without them.

No doubt there will be some people here who feel differently about it.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Nimmi Srivastav wrote:

Can someone kindly clarify the distinction
between long jumps and gotos?
They really are *not* comparable.
Why is one frowned upon and not the other?
the setjmp/longjpm functions are *not* for new or amateur programmers.
Is there really any situation
where use of longjmp becomes inevitable?
The setjmp/longjmp mechanism
is the way that C programmers "throw" exceptions.
Unfortunately, it does *not* clean-up after you.
Memory allocated from free storage after setjmp
will not be free''d.
A real-life code snippet would surely help.



Take a look at the man page:

http://www.rahul.net/cgi-bin/userbin...gjmp&section=3

The following code fragment indicates the flow of control of
the setjmp() and longjmp() combination:

/function declaration/
...
jmp_buf my_environment;
...
if (setjmp(my_environment)) {
/* register variables have unpredictable values */
code after the return from longjmp
...
} else {
/* do not modify register vars in this leg of code */
this is the return from setjmp
...
}


ni*************@yahoo.com (Nimmi Srivastav) wrote:
# Can someone kindly clarify the distinction between long jumps and
# gotos? Why is one frowned upon and not the other? Is there really
# any situation where use of longjmp becomes inevitable? A real-life
# code snippet would surely help.

(1) You can''t goto from one function to another. You can longjmp from a
function invocation anywhere upward in the protocol stack.

(2) ANS C doesn''t have label variables, but jmp_bufs are variables.

In my recursive ascent parser, error recovery abandons the current parse
before recoverying and restarts from the initial state (more or less).
Rather than have initial function in the parser check on return if a
parse error occurs, I just longjmp from the error detection to the error
recovery.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I''m not even supposed to be here today.


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

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