setjmp和logjmp是配合使用的,他們可以實(shí)現(xiàn)在不同函數(shù)間的跳轉(zhuǎn)
- setjmp設(shè)置跳轉(zhuǎn)點(diǎn)拨齐,第一次設(shè)置跳轉(zhuǎn)點(diǎn)柜裸,返回值是0
- longjmp跳轉(zhuǎn)到setjmp設(shè)置的跳轉(zhuǎn)點(diǎn)之后, 再執(zhí)行setjmp, 這時(shí)setjmp會(huì)返回1
看下面栗子:
#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
void fun_sen() {
printf("fun_sen runs..\n");
longjmp(buf, 33);
}
void fun() {
printf("fun before runs..\n");
fun_sen();
printf("fun after runs..\n");
}
int main(void)
{
int i;
if (!setjmp(buf))
{
fun();
} else {
printf("main runs..\n");
}
return 0;
}
image.png