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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ue4 弱指针_智能指针如何实现自动释放

    ue4 弱指针_智能指针如何实现自动释放原创文章,转载请注明出处。UE4也有一套智能指针库,整理了一下做个介绍。也请大家做补充。共享指针/共享引用/弱指针/注意事项一.TSharePtr1.如何创建一个TSharePtr2.TSharePtr如何进行类型转换1)TSharePtr转TSharePtr2)ConstTSharePtr转TSharePtr3)TSharePtr转TShareRef3.使用注意事项1)TSharePtr2)类型转换二.TShareRef1.如何创建一个TShareRef2.TShareRef如何进行类型转换1)TS

    2022年10月4日
  • 校园兼职平台青团社获数亿元B+轮融资,蚂蚁⾦服领投

    校园兼职平台青团社获数亿元B+轮融资,蚂蚁⾦服领投

    2021年7月1日
  • idea插件开发指南_vip

    idea插件开发指南_vipIDEA最全实用插件(VIP典藏版)

    2022年9月4日
  • ride运行报错_chrome OS

    ride运行报错_chrome OShttp://chromedriver.storage.proxy.ustclug.org/index.html下载地址,注意需要与chrome版本对应chromedriver下载解压后,放在chrome安装目录下,查看安装目录chrome://version/最后配置环境变量,chrome安装目录配置在path后重启(我是win10,开始配置在系统变量中重启后无效,后来在用户变量…

    2022年11月2日
  • VLAN概念和作用的自我总结

    VLAN概念和作用的自我总结什么是VLAN:   一个VLAN是跨越多个物理LAN网段的逻辑广播域,人们设计VLAN来为工作站提供独立的广播域,这些工作站是依据其功能、项目组或应用而不顾其用户的物理位置而逻辑分段的。一个VLAN=一个广播域=逻辑网段VLAN的优点和安装特性:VLAN的优点:   ·安全性。一个VLAN里的广播帧不会扩散到其他VLAN中。   ·网络分段。将物理网段按

    2022年8月10日
  • 我们究竟在为谁而工作?80%的人没有搞懂.

    首问: 我们的价值观是什么?首先要明白自己的价值观是什么?我们想过什么样的生活,我们将来要成为什么样的人?我现在的工作我满意吗?我现在的工作态度满意吗? 我觉得首先要问清楚自己这些…

    2021年6月21日

发表回复

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

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