在C中有时我们会使用goto语句用于执行跳转,但是不能跨越函数
#include <stdio.h>
void func2()
{
int num = 0;
dst2:
if (num > 0)
{
printf("func1()\n");
func3();
}
if (num == 1)
return;
num++;
goto dst2;
}
void func3()
{
}
void func1()
{
dst1:
func2();
goto dst2;//error
}
int main()
{
func1();
return 0;
}
而在深层嵌套函数中的跳转,可以使用setjmp和longjmp函数。
这两个函数对于处理发生在深层嵌套函数中出错的情况是非常有用的。
函数原型:
#include <setjmp.h> int setjmp(jmp_buf env); 返回值:若直接调用则返回0,若从longjmp调用返回则返回非0值 void longjmp(jmp_buf env, int val);参数说明:
实例:
#include <stdio.h>
#include <setjmp.h>
jmp_buf jmpbuffer;
int times = 0;
void func3()
{
times++;
longjmp(jmpbuffer, 3);
}
void func2()
{
func3();
}
void func1()
{
int r;
r = setjmp(jmpbuffer);
printf("%d\n", r);
if (times == 1)
return;
func2();
}
int main()
{
func1();
return 0;
}
实例:
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf jmpbuffer;
void func1()
{
int auto_data = 0;
static int static_data = 0;
volatile int volatile_data = 0;
if (setjmp(jmpbuffer) != 0)
{
printf("after jmp:\n");
printf("auto: %d\n", auto_data);
printf("static: %d\n", static_data);
printf("volatile: %d\n", volatile_data);
exit(0);
}
printf("auto: %d\n", auto_data);
printf("static: %d\n", static_data);
printf("volatile: %d\n", volatile_data);
auto_data++;
static_data++;
volatile_data++;
longjmp(jmpbuffer, 3);
}
int main()
{
func1();
return 0;
}不带优化编译gcc
带优化编译 gcc -O
存放在存储器中的变量将具有longjmp时的值,而在cpu寄存器中的变量则恢复为调用setjmp时的值,
不进行优化时,自动变量存放在存储器中,
优化后,自动变量存放寄存器中,这就是优化编译后auto_data值变为0的原因。
原文:http://blog.csdn.net/aspnet_lyc/article/details/32184413