学习网络上的文章:http://blog.sina.com.cn/s/blog_7ffcb1410100s0ut.html
然后我自己做了一下简单地验证:
[root@localhost test]# cat testjmp.c #include<stdio.h> #include<setjmp.h> jmp_buf ebuf; int ftaste(); int main() { int i; fprintf(stderr,"1\n"); i=setjmp(ebuf); if (i==0) { ftaste(); fprintf(stderr,"This will not be run\n"); }else{ fprintf(stderr,"Now i is %d\n",i); } fprintf(stderr,"%d\n",i); return 0; } int ftaste() { fprintf(stderr,"2\n"); longjmp(ebuf,3); } [root@localhost test]#
改成这样后,运行的结果是:
[root@localhost test]# ./testjmp.o
1
2
Now i is 3
3
也就是说,设置好 setjmp 以后
longjmp 会跳转到 setjmp 之后的下一行的位置
setjmp与longjmp用法示例
本文通过一个简单的C语言程序示例,演示了setjmp与longjmp函数的基本用法。通过设置jmp_buf类型的缓冲区并调用setjmp初始化,再利用longjmp返回到该缓冲区所记录的位置,展示了非局部跳转的功能。
5145

被折叠的 条评论
为什么被折叠?



