|
代码:
#include "apue.h"
#include <setjmp.h>
static void f1(int, int, int);
static void f2(void);
static jmp_buf jmpbuffer;
int
main(void)
{
int count;
register int val;
volatile int sum;
count = 2; val = 3; sum = 4;
if (setjmp(jmpbuffer) != 0) //返回0表示已经储存好当前的堆栈环境,非0值表示由longjmp跳跃回来
{
printf("after longjmp: count = %d, val = %d, sum = %d\n",count, val, sum);
exit(0);
}
count = 97; val = 98; sum = 99;
/* changed after setjmp, before longjmp */
f1(count, val, sum); /* never returns */
}
static void
f1(int i, int j, int k)
{
printf("in f1(): count = %d, val = %d, sum = %d\n", i, j, k);
f2();
}
static void
f2(void)
{
longjmp(jmpbuffer, 1);//void longjmp(jmp_buf env, int val);
}
/*
val参数提供了setjmp()的返回值,这个值不能为0,如果为0,系统会用1来代替。
*/
这个东西,先把变量设置成234,然后用setjmp储存,然后改成97.98.99,打印,然后再返回到setjmp那里,再打印,。
按道理来说应该先打印97.98.99再打印2.3.4的。
但是
lucifer@LovelyBaby:~/CC/Unix$ cc testjmp.c
lucifer@LovelyBaby:~/CC/Unix$ ./a.out
in f1(): count = 97, val = 98, sum = 99
after longjmp: count = 97, val = 98, sum = 99
lucifer@LovelyBaby:~/CC/Unix$
我又哪里错了阿? |
|