setjmp(),longjmp 例子
mail:bookworepeng@Hotmail.com
qq:196568501
author:Driver Monkey
phone:13410905075
#include <stdio.h> #include <setjmp.h> #include <stdlib.h> static jmp_buf jmpbuffer1; static jmp_buf jmpbuffer2; static void longjump_f1(void); static void longjump_f2(void); int main(void) { int test_auto = 0x55; static int test_static = 0x55; volatile int test_volatile = 0x55; int returnv = 0; static int flag = 0; if((returnv = setjmp(jmpbuffer1)) != 0) { printf("after jump1:\n"); printf("test_auto = 0x%x,test_static = 0x%x,test_volatile = 0x%x\n" ,test_auto,test_static,test_volatile); printf("returnv = %d\n", returnv); printf("flag = %d\n", flag); flag = 1; } if((returnv = setjmp(jmpbuffer2)) != 0) { printf("after jump2:\n"); printf("test_auto = 0x%x,test_static = 0x%x,test_volatile = 0x%x\n" ,test_auto,test_static,test_volatile); printf("returnv = %d\n", returnv); printf("flag = %d\n", flag); flag = 2; } test_auto = 0xaa; test_static = 0xaa; test_volatile = 0xaa; switch(flag) { case 0: longjump_f1(); break; case 1: longjump_f2(); break; default: exit(0); } } void longjump_f1(void) { printf("longjum_f1()++\n"); longjmp(jmpbuffer1,30); printf("longjum_f1()--\n"); } void longjump_f2(void) { printf("longjum_f2()++\n"); longjmp(jmpbuffer2,40); printf("longjum_f2()--\n"); }