是否可以一次调用一次setjmp()多次执行longjmp()? [英] Is it allowed to do longjmp() multiple times for one setjmp() call?

查看:95
本文介绍了是否可以一次调用一次setjmp()多次执行longjmp()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的理解中, setjmp() longjmp()的典型用法是异常处理(在 libpng 中的用法应该是著名的这样的示例),那么最多只有一个调用 setjmp()调用一次 longjmp().

In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call.

这样是否可以安全地多次允许一次 setjmp()调用 longjmp()?

Is it safely allowed to do longjmp() multiple times for one setjmp() call like this?

#include <stdio.h>
#include <setjmp.h>

jmp_buf jb;
int i;

int main(void) {
    i = 0;
    setjmp(jb);
    printf("%d\n", i);
    i++;
    if (i < 10) longjmp(jb, 1);
    return 0;
}

输出:

0
1
2
3
4
5
6
7
8
9

我从这次执行中成功获得了预期的输出,但是可以保证吗?还是将 longjmp()一次用于 jmp_buf 无效?

I successfully got the expected output from this execution, but is this guaranteed? Or will jmp_buf invalidated when longjmp() is once used for that?

setcontext-Wikipedia 说:它们可能被视为setjmp/的高级版本.longjmp;而后者仅允许单个非本地跳转到堆栈上",但我没有找到

setcontext - Wikipedia says "They may be viewed as an advanced version of setjmp/longjmp; whereas the latter allows only a single non-local jump up the stack", but I didn't find descriptions that disallow multiple usage of longjmp() like this from N1570 7.13 Nonlocal jumps <setjmp.h>.

我不建议使用 setjmp() longjmp(),但是我想知道在使用循环语句( for while do-while )和 goto 语句被禁止,但使用 setjmp() longjmp()在某些编程测验中没有被禁止.(使用递归可能是此类测验的答案,但是在尝试处理需要多次迭代的大数据时,它具有堆栈溢出的风险)

I know that using setjmp() and longjmp() is discouraged, but I am wondering if they can be used as a workaround when using loop statements (for, while, do-while) and goto statements is banned but using setjmp() and longjmp() is not banned in some programming quiz. (using recursion may be answers for this type of quiz, but it has risk of stack overflow when trying to deal with large data that require many iterations)

推荐答案

这样安全地允许一次调用一次setjmp()多次执行longjmp()吗?

Is it safely allowed to do longjmp() multiple times for one setjmp() call like this?

可以构造一个严格符合标准的程序,该程序多次调用 longjmp()以返回到同一 setjmp()调用的点.它归结为抽象机的状态,包括内存内容,尤其是 jmp_buf 的状态,其中 setjmp()调用记录了返回到该状态所需要的状态通话的重点.该标准规定

It is possible to construct a strictly conforming program that calls longjmp() multiple times to return to the point of the same setjmp() call. It comes down to the state of the abstract machine, including memory contents, and especially the state of the jmp_buf in which a setjmp() call records the state required to return to the point of that call. The standard specifies that

所有可访问对象都具有值,并且所有其他组成部分从 longjmp 函数开始的那一刻起,抽象机就具有状态被称为,除了[...可以避免或制造的细节无关紧要...].

All accessible objects have values, and all other components of the abstract machine have state, as of the time the longjmp function was called, except that [... details that can be avoided or made immaterial ...].

(C2018 7.13.2.1/3)

(C2018 7.13.2.1/3)

尤其是,这意味着 longjmp()调用不得更改从其获取信息的 jmp_buf 的值,并且不能存在任何隐藏状态 longjmp()可以在其他位置进行更新,以将相应的 setjmp()标记为已用完.如果机器状态允许进行 longjmp()调用,则等效的 longjmp()调用必须在产生的第二(或第三, etc .)从相应的 setjmp()调用返回.

In particular, that means that the longjmp() call must not change the value of the jmp_buf from which it gets its information, and there cannot be any hidden state elsewhere that longjmp() could update to mark the corresponding setjmp() as being used up. If machine state permits a conforming longjmp() call, then an equivalent longjmp() call must still be conforming after the resulting second (or third, etc.) return from the corresponding setjmp() call.

这篇关于是否可以一次调用一次setjmp()多次执行longjmp()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆