这里用最简单直接的描述:这两组函数是用于实现类似vscode全局的标签跳转功能,setjmp负责埋下标签,longjmp负责标签跳转。
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>jmp_buf envbuf1;
jmp_buf envbuf2;void func_label1()
{int ret = setjmp(envbuf1);printf("func label1 ret:%d.\n",ret);
}void func_label2()
{int ret = setjmp(envbuf2);printf("func label2 ret:%d.\n",ret);
}void test1()
{printf("test1 called.\n");longjmp(envbuf1,100);printf("test1 never run here.\n");
}void test2()
{printf("test2 called.\n");longjmp(envbuf1,200);printf("test2 never run here.\n");
}void test3()
{printf("test3 called.\n");longjmp(envbuf1,300);printf("test3 never run here.\n");
}void test4()
{printf("test4 called.\n");longjmp(envbuf2,400);printf("test4 never run here.\n");
}void test5()
{printf("test5 called.\n");longjmp(envbuf2,500);printf("test5 never run here.\n");
}void main()
{printf("==========Start process======\n");func_label1();func_label2();test1();//label1test2();test3();test4();//label2test5();printf("==========End process======\n");
}
运行效果:
从这个运行结果可以清楚的看到两个标签的跳转效果!