谁给我讲讲longjmp,我给弄糊涂了!
下面的程序运行结果和预期相去甚远,为什么?
#include<stdio.h>
#include<signal.h>
#include<setjmp.h>
jmp_buf buf;
void fun(int s)
{
puts("I love you!");
longjmp(buf,0);
puts("welcome to here!");
}
int main()
{
signal(SIGINT,&fun);
if(setjmp(buf))
{
puts("!!!!!Good!!!!!");
return 0;
}
//loop:
else puts("First thought the main");
for(;;);
}
输入Ctrl+C为什么没有能打印出!!!!!Good!!!!!,不是应该跳转到这前面吗?
问题点数:100、回复次数:2Top
1 楼mathe()回复于 2004-12-03 12:49:16 得分 50
说明longjmp不能在signal函数里面处理呀。
Try
int main()
{
if(setjmp(buf))
{
puts("!!!!!Good!!!!!");
return 0;
}
//loop:
else puts("First thought the main");
fun(1);
}Top
2 楼BlueSky2008(懒惰是程序员的美德)回复于 2004-12-03 13:09:52 得分 50
setjmp returns 0 after saving the stack environment. If setjmp returns as a result of a longjmp call, it returns the value argument of longjmp, or if the value argument of longjmp is 0, setjmp returns 1. There is no error return.
Top




