setjmp()返回值问题

lockhall 2007-04-20 11:33:52
#include<iostream.h>
#include<setjmp.h>

//using namespace std;

class rainbow
{
public:
rainbow()
{
cout<<"rainbow()"<<endl;
}
~rainbow()
{
cout<<"~rainbow()"<<endl;
}
};

jmp_buf kansas;

void OZ()
{
rainbow RB;
int i = 0;
for(i;i < 3;i++)
{
cout<<"there's no place like home"<<endl;
}
longjmp(kansas,47);
}
int main()
{
if(setjmp(kansas) == 0)
{
cout<<"tornado ,witch,munchkins...\n";
OZ();
}
else
{
cout<<"Auntie Em"<<"i had the strongest dream"<<endl;
}
system("PAUSE");
return 0;
}

问题1:我在DEV++中,用using namespace std;居然编译不通过,为何?

问题2:执行结果是:
tornado ,witch,munchkins...
rainbow()
there's no place like home
there's no place like home
there's no place like home
Auntie Em i had the strongest dream

在OZ()中调用longjmp后,应该是跳转到if(setjmp(kansas) == 0)这里了,这时候为何执行了else,难道setjmp(kansas)此时返回值不等于0?那应该等于多少?
...全文
306 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lockhall 2007-04-23
  • 打赏
  • 举报
回复
OK.3ks
zydolphin 2007-04-21
  • 打赏
  • 举报
回复
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
----MSDN
jixingzhong 2007-04-21
  • 打赏
  • 举报
回复
setjmp返回longjmp的value自变量值,
就是你指定的 47
jixingzhong 2007-04-21
  • 打赏
  • 举报
回复
using namespace std; 请使用C++新的头文件格式,即不带 .h 的包含方式
snprintf 2007-04-21
  • 打赏
  • 举报
回复
问题1: VS2005 下没有这个问题. 可以正常运行.
问题2: 如果setjmp返回一个调用longjmp的结果, 它返回longjmp的value自变量. 因此这里返回47, 你可以用下面的程序测试一下:
#include<iostream>
#include<setjmp.h>

using namespace std; // VS2005 编译通过!

class rainbow
{
public:
rainbow()
{
cout<<"rainbow()"<<endl;
}
~rainbow()
{
cout<<"~rainbow()"<<endl;
}
};

jmp_buf kansas;

void OZ()
{
rainbow RB;
int i = 0;
for(i;i < 3;i++)
{
cout<<"there's no place like home"<<endl;
}
longjmp(kansas,47);
}
int main()
{
int x;
if((x=setjmp(kansas)) == 0)
{
cout<<"tornado ,witch,munchkins...\n";
OZ();
}
else if (x==47) // 这里运行结果和原来一样, 说明 返回了 47
{
cout<<"Auntie Em"<<"i had the strongest dream"<<endl;
}
system("PAUSE");
return 0;
}
mymtom 2007-04-21
  • 打赏
  • 举报
回复
问题1
#include <iostream.h>
改为
#include <iostream>

问题2
longjmp(kansas,47) 跳转到if(setjmp(kansas) == 0) 并且 setjmp(kansas) 返回 47, 也就是longjmp 的第二个参数!

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧