69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
void test();
jmp_buf buf1,buf2;
int main(void)
{
if(setjmp(buf1))
{
printf("longjmp back main.\n");
longjmp(buf2,2);
}
else {
printf("first go here.\n");
test();
}
exit(0);
}
void test()
{
static int m=10;
printf("test function.\n");
m+=20;
if(setjmp(buf2)==0)
longjmp(buf1,1);
printf("m:%d",m);
}
.file "test3.c"
.comm buf1,156,32
.comm buf2,156,32
.section .rodata
.LC0:
.string "first go here."
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $buf1, (%esp)
call _setjmp
testl %eax, %eax
je .L2
movl $2, 4(%esp)
movl $buf2, (%esp)
call longjmp
.L2:
movl $.LC0, (%esp)
call puts
call test
movl $0, (%esp)
call exit
.size main, .-main
.section .rodata
.LC1:
.string "test function."
.LC2:
.string "m:%d"
.text
.globl test
.type test, @function
test:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $.LC1, (%esp)
call puts
movl m.2223, %eax
addl $20, %eax
movl %eax, m.2223
movl $buf2, (%esp)
call _setjmp
testl %eax, %eax
jne .L5
movl $1, 4(%esp)
movl $buf1, (%esp)
call longjmp
.L5:
movl m.2223, %edx
movl $.LC2, %eax
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
leave
ret
.size test, .-test
.data
.align 4
.type m.2223, @object
.size m.2223, 4
m.2223:
.long 10
.ident "GCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13)"
.section .note.GNU-stack,"",@progbits
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
void test();
jmp_buf buf1,buf2;
void main(void)
{
if(setjmp(buf1))//setjmp返回0执行test;(2)-值为1,成立
{
printf("longjmp back main.\n");
longjmp(buf2,2);//调回buf2时,原来的test调用保存的栈内容已经消亡,再进行函数跳出时候出现错误《——错误就在这里
}
else {
printf("first go here.\n");
test();
}
//exit(0);
}
void test()
{
int m=10;
printf("test function.\n");
m+=20;
if(setjmp(buf2)==0)//成立
longjmp(buf1,1);//调回buf1处,值为1,到(2),注意在这里test调用也就算结束了
printf("m:%d",m);//把static去掉后,你会看到结果不是10也不是30是个随机值,也就说明不是继续上次test调用后执行
}
#include <stdio.h>
#include <setjmp.h>
jmp_buf mark;
int i=0;
void c() {
printf( "c in\n");
if (1==i%2) longjmp( mark, i );
printf( "c out\n");
}
void b() {
printf( "b in\n");
c();
printf( "b out\n");
}
void a() {
printf( "a in\n");
b();
printf( "a out\n");
}
void main( void )
{
int jmpret;
jmpret = setjmp( mark );
printf( "Start %d\n",jmpret);
if( jmpret == 0 ) {
printf( "First\n");
a();
i++;
a();
} else {
printf( "Second\n");
}
}
//Start 0
//First
//a in
//b in
//c in
//c out
//b out
//a out
//a in
//b in
//c in
//Start 1
//Second
//