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.
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
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 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>.
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)