程序跳轉之longjmp
時(shi)間:2018-09-29 來源(yuan):未知
#include <stdio.h>
#include <setjmp.h>
jmp_buf env1;
int func(void);
int main(int argc, const char *argv[])
{
int ret = 0;
printf("Before setjmp.\n");
// 保存當前的環(huan)境變量,longjmp 依賴此環(huan)境變量進行跳轉(zhuan)
// 此函數(shu)的返回(hui)值(zhi),非跳轉時,返回(hui)值(zhi)為0,當對方(fang)跳轉來,
// 返回值為(wei)longjmp的(de)參數
ret = setjmp(env1);
if(ret == 0)
{
func();
}
else
{
printf("+++++++++, ret = %d\n", ret);
}
printf("After setjmp.\n");
return 0;
}
int func(void)
{
printf("In func.\n");
//完成程序的(de)跳轉,具有全局的(de)作用范圍,函數之間(jian),文件之間(jian),線程與線程之間(jian)
longjmp(env1, 3);
return 0;
}

