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.

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • lua sort排序_python中列表排序的用法

    lua sort排序_python中列表排序的用法”’lua中对table的排序一般是用lua自带的table.sort()函数排序,一般不采用自己写的排序的方式,以下来说一说table.sort()排序和在工作中遇到的问题1.排序的方式table.sort(tbl,function(a,b)returna>bend)以上是一个简单的例子,得到的效果是对于待排序的数据的一个升序,你这样认为就是错了,例如a

  • java中数组转列表_Java数组转list

    java中数组转列表_Java数组转list1.javajdk提供的Arrays.asList(T…a)方法publicstaticvoidmain(String[]args){String[]strArray={“a”,”b”,”c”};List<String>strList=Arrays.asList(strArray);Sy…

  • date函数举例_初中数学基础知识整理

    date函数举例_初中数学基础知识整理Date类型:1.Date.parse()接收一个表示日期的字符串参数,然后再根据这个字符串返回响应的日期的毫秒数;如:创建一个日期:2.Date.UTC():也返回表示日期的毫秒数;但是其参数分别

  • SVG可伸缩的矢量图形「建议收藏」

    SVG可伸缩的矢量图形「建议收藏」SVG可伸缩的矢量图形

  • grid布局方式_grid网格布局

    grid布局方式_grid网格布局GridBagConstraints特征:由GridBagConstraints类实现的布局管理器称为网格组布局管理器,它实现了一个动态的矩形网格,这个矩形风格由无数个矩形单元格组成,每个组件可以占用一个或多个这样的单元格。动态矩形网格:可以根据实际需要随意增减矩形网格的行数和列数。它实现的矩形网格的绘制方向由容器决定,网格的索引从0开始。下面写一个测试方法来讲解GridBagC

  • VMware虚拟机安装黑群晖7.0教程

    VMware虚拟机安装黑群晖7.0教程教程仅供参考,不当之处多多理解。该篇教程主要讲解黑群晖(DS918+)的安装Tip:本教程本教程只用于个人学习使用,有条件,长期使用的朋友推荐从正规官方渠道入手。1.首先安装VMware虚拟机双击安装文件进行安装…

发表回复

您的电子邮箱地址不会被公开。

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