如何与QUOT;转到"成在C不同的功能? [英] How to "goto" into different function in c?

查看:110
本文介绍了如何与QUOT;转到"成在C不同的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我试图模拟用C汇编code。

下面是C code:

int main()
{
   test();
main_next:
   printf("hello, world);
}

void test()
{
     goto main_next;
}

试图编译这个code(在Linux 32位,GCC 4.6.3),我得到这个错误:

Trying to compile this code (Linux 32 bit, gcc 4.6.3), I got this error:

 error: label ‘main_randomtag_next’ used but not defined

有谁知道怎么做这种用C的过程间的goto吗?

Does anyone know how to do this kind of inter-procedural goto in C?

感谢您!

推荐答案

转到函数之间,如果你想堆没有任何意义。将在堆栈上什么,当你跳?源和目标功能可能有不同的参数和不同的返回值。谁将会在新的函数返回?将其返回值,甚至是有意义的来电?称为源函数,而不是目标函数调用者。

But what about the children? stack?

goto between functions doesn't make any sense if you think about the stack. What will be on the stack when you jump? The source and destination functions could potentially have different arguments and a different return value. Who will the new function return to? Will its return value even make sense to the caller? The caller called the source function, not the destination function.

仔细考虑您的例子:

int main()
{
   test();
main_next:
   printf("hello, world);
}

void test()
{
     goto main_next;
}

转到执行时会发生什么?我presume你想要这个跳的堆栈的回调用的main()功能。在转到将有效地等同于收益,更改调用堆栈:

What happens when the goto executes? I presume you'd want this to jump up the stack back to the calling main() function. The goto would effectively be the same as a return, changing the call stack from:

main()                            main()
|                   to            
+--> test()                       

但是,如果你想跳转到一个函数,是不是在调用栈?然后怎样呢?

But what if you wanted to jump to a function that isn't in the call stack? What then?

一个不同的跨pretation是,转到将取代现有的测试()与一个叫到的main()。调用堆栈会从改变:

A different interpretation is that the goto would replace the existing test() call with one to main(). The call stack would change from:

main()                            main()
|                   to            |
+--> test()                       +--> main()

现在的main()是递归调用本身,而较低的的main()将返回到上的main()—谁,顺便说一句,期待一个无效返回值,但将要接收 INT

Now main() is recursively calling itself, and the lower main() will return to the upper main()—who, by the way, is expecting a void return value but is going to receive an int.

的setjmp / 的longjmp 。这允许你保存和恢复堆栈上下文非局部goto,让您在函数调用之间跳转。

The closest you can get is with setjmp / longjmp. These allow you to save and restore the stack context for nonlocal goto, allowing you to jump between function calls.

的setjmp 的longjmp 围绕我描述的(一)节能问题get和还原时全栈的上下文跳跃,和(b)不容许跳跃如果堆栈上下文不再有效。我从手册页(重点煤矿):

setjmp and longjmp get around the problems I described by (a) saving and restoring the full stack context when jumping, and (b) not allowing jumps if the stack context is no longer valid. I quote from the man page (emphasis mine):

的setjmp()和longjmp(3)用于处理错误,并在程序的低级别中断子程序中遇到有用的。那么setjmp()保存在由ENV longjmp的(3)堆栈环境/环境中供以后使用。 堆栈上下文将失效,如果它被称为setjmp的函数()的回报。

setjmp() and longjmp(3) are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp() saves the stack context/environment in env for later use by longjmp(3). The stack context will be invalidated if the function which called setjmp() returns.

要换个说法,的longjmp 基本上是等价的C中的抛出异常的。一个低级别的功能,可以放松身心的调用堆栈,并在更高的层次功能恢复执行。

To put it another way, longjmp is basically the C equivalent of throwing an exception. A low-level function can unwind the call stack and resume execution at a much higher level function.

这也是非常棘手的使用,很少是个好主意。再次,从手册页:

It's also awfully tricky to use, and rarely a good idea. Again, from the man page:

的setjmp()和此时sigsetjmp()使程序难以理解和维护。如果可能的话,应使用的替代品。

setjmp() and sigsetjmp() make programs hard to understand and maintain. If possible an alternative should be used.

这篇关于如何与QUOT;转到"成在C不同的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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