no jump_jump out

no jump_jump outCprovidesaformofuser-levelexceptionalcontrolflow,calledanonlocaljump,thattransferscontroldirectlyfromonefunctiontoanothercurrentlyexecutingfunctionwithouthavingtogothroug

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
C provides a form of user-level exceptional control flow, called a nonlocal jump, that transfers control directly from one function to another currently executing function without having to go through the normal call-and-return sequence.

      An important application of nonlocal jumps is to permit an immediate return from a deeply nested function call, usually as a result of detecting some error condition. If an error condition is detected deep in a nested function call, we can use a nonlocal jump to return directly to a common localized error handler instead of laboriously unwinding the call stack. We present an example here.

#include "csapp.h"
#include <iostream>
using namespace std;

jmp_buf buf;

int error1 = 0;
int error2 = 1;

void foo(void), bar(void);

int main()
{
        int rc;
    
		rc = setjmp(buf);
		if (rc == 0)
		        foo();
		else if (rc == 1)
				cout << "Detected an error1 condition in foo" << endl;
		else if (rc == 2)
				cout << "Detected an error2 condition in foo" << endl;
		else
				cout << "Unknown error condition in foo" << endl;
		exit(0);
}

// Deeply nested function foo
void foo(void)
{
		if (error1)
				longjmp(buf, 1);
		bar();
}

void bar(void)
{
		if (error2)
				longjmp(buf, 2);
}

In the above program, the main routine first calls setjmp to save the current calling environment, and then calls function  foo, which in turn calls function bar. If  foo or bar encounter an error, they return immediately from the setjmp via a longjmp call. The nonzero return value of the setjmp indicates the error type.

      Another important application of nonlocal jumps is to branch out of a signal handler to a specific code location, rather than returning to the instruction that was interrupted by the arrival of the signal. The following program uses signals and nonlocal jumps to do a soft restart whenever the user types ctrl-c at the keyboard. The sigsetjmp and siglongjmp functions are versions of setjmp and longjmp that can be used by signal handlers.

#include "csapp.h"
#include <iostream>
using namespace std;

sigjmp_buf buf;

void handler(int sig)
{
    siglongjmp(buf, 1);
}

int main()
{
		Signal(SIGINT, handler);

		if (!sigsetjmp(buf, 1))
				cout << "starting" << endl;
		else
				cout << "restarting" << endl;

		while(1) {
				Sleep(1);
				cout << "processing..." << endl;
		}
		exit(0);
}

The initial call to the sigsetjmp function saves the stack and signal context when the program first starts. The main routine then enters an infinite processing loop. When the user types ctrl-c, the shell sends a SIGINT signal to the process, which catches it. Instead of returning from the signal handler, which would pass back control back to the interrupted processing loop, the handler performs a nonlocal jump back to the beginning of the main program. When we ran the program on our system, we got the following output:

starting
processing…
processing…
restarting          User hits ctrl-c
processing…
restarting          User hits ctrl-c
processing…

Source:

Randal E. Bryant, David R. O’Hallaron(2011). COMPUTER SYSTEMS A Programmer’s Perspective (Second Edition).Beijing: China Machine Press.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196806.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql replace into 的使用情况

    mysql replace into 的使用情况

    2021年11月27日
    38
  • 小程序-微信开发者工具使用教程_小程序开发教程

    小程序-微信开发者工具使用教程_小程序开发教程一、开始开发小程序的第一步,你需要拥有一个小程序帐号,通过这个帐号你就可以管理你的小程序。跟随这个教程,开始你的小程序之旅吧!二、申请帐号进入小程序注册页 (https://mp.w

    2022年8月1日
    4
  • Android中联系人使用

    我8月份的时候接触过联系人这里,看了很多文章,把我弄蒙了,今天突然发现这篇文章,不错,如果我以后涉及到这方面的业务,会多来学习下,作者博客地址和英文原文地址都放在最下面了。前阵子搞短信,发现Android1.x至2.0版本联系人数据库很多地方做了更改,且关于这方面的资料也比较少,所以找到一篇文章稍作翻译了下,以供大家参考,该文将分三部分发布。WorkingWithAndro

    2022年4月8日
    40
  • 红旗linux又活过来了_grub引导windows

    红旗linux又活过来了_grub引导windows红旗Linux桌面版4.0软盘启动硬盘安装过程图解(RedFlagLinux4.0)一,准备工作:1,购买或下载红旗Linux桌面版4.0的安装光盘或镜像文件,下载地址:http://www.doczj.com/doc/cc20134b852458fb770b56ff.html/xiazai/xiazai.php?id=13252,在硬盘中至少留2个分区给安装系统用,挂载点所用分区推荐…

    2022年8月20日
    9
  • 香农编码的matlab实现总结_matlab简单代码实例

    香农编码的matlab实现总结_matlab简单代码实例用MATLAB编程实现香农编码实验四、用MATLAB编程实现香农编码⒈实验目的和要求(1)了解信源变长码的编码方法(2)掌握香农编码方法(3)掌握MATLAB的编程⒉实验主要内容使用MATLAB语言和excel对以下信源进行香农编码,并求出平均码长和编码效率。3.实验原理Ⅰ.香农编码方法将信源发出的N个消息符号按其概率的递减次序排列按下式计算第个消息的二进制代码组的码长,并取整计算第个消息的累加概…

    2025年10月21日
    6
  • mysql datetime与timestamp区别

    mysql datetime与timestamp区别datetime:1、与时区无关,存入的是什么值就是什么值,不会根据当前时区进行转换2、从mysql5.6.4中,可以存储小数片段,最多到小数点后6位,显示时格式为yyyy-MM-ddHH:mm:ss[.222222]mysql5.5中,没有小数片段。所以,我再从5.6版本迁移到5.5版本时,因为生成的sql中datetime(6),所以无法导入数据库。3、存储

    2025年7月15日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号