将 jmp_buf 声明为指针
我试图将 jmp_buf 定义为指针并在嵌套的 longjmp(s) 中使用它。如下所示:
...
jmp_buf *bfj;
...
然后编写 if else:
if( setjmp(*bfj) == 0){
DS[SP-2].int_val=(int)bfj;;
//to store the bfj
}else {}
并在其他地方使用存储的 bfj 到 longjmp
bfj = (jmp_buf *)DS[TOP].int_val;
longjmp(*bfj,1);
,其中 DS[TOP].int_val 是我存储它的位置。 看起来很清楚,我想使用存储的 bfj 进行嵌套 goto 和返回。 但是当我尝试调试时,我得到“未处理的异常”。我从一开始就明白了这一点:
if( setjmp(*bfj) == 0)
如果有人能告诉我解决方案,我会很高兴。
I'm tring to define jmp_buf as pointer and using it in nested longjmp(s).as follow:
...
jmp_buf *bfj;
...
and then writing if else:
if( setjmp(*bfj) == 0){
DS[SP-2].int_val=(int)bfj;;
//to store the bfj
}else {}
and somewhere else using the stored bfj to longjmp
bfj = (jmp_buf *)DS[TOP].int_val;
longjmp(*bfj,1);
where DS[TOP].int_val is where I stored it.
as it may seems clear,I want to do nested gotos and returns using stored bfj.
but well when I try to debug I get "unhandeled exception". I get this at the very starting point:
if( setjmp(*bfj) == 0)
I would be pleased if someone would tell the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码来看,您实际上并未为
jmp_buf
分配内存。您可以执行以下操作:new
动态分配您的jmp_buf
,并在使用完毕后将其删除
jmp_buf
放在堆栈jmp_buf bfj;
上,当您需要它的指针时,您可以使用&bfj
获取它的地址。因此,#1 看起来像:
而 #2 看起来像:
另一个潜在的问题是,您永远不应该将指针强制转换为
int
,因为指针可能比 int 占用更多内存(这种情况发生在常见的 64 位编程模型)。如果无法直接存储指针,则应使用intptr_t
代替。From your code, you are not actually allocating memory for your
jmp_buf
. There are a couple of things you can do:jmp_buf
withnew
and you will want todelete
it when you are done with itjmp_buf
on the stackjmp_buf bfj;
and when you want it's pointer, you would take it's address with&bfj
.So, #1 would look like:
while #2 would look like:
Another potential issue is that you should never cast a pointer to an
int
as a pointer may take more memory then an int (this happens on the common 64 bit programming models). If you can't store the pointer directly, you should useintptr_t
instead.