The following code just ends up printing "5"
#include <iostream>
#include <setjmp.h>
static jmp_buf buf;
float funcB()
{
setjmp(buf);
return 1.6f;
}
int funcA()
{
longjmp(buf,5);
std::cout<<"b";
return 2;
}
int main()
{
funcB();
std::cout<<funcA();
}
But this doesn't make any sense, as setjmp is returning 5, not either function... Don't worry, I'm not using this code anywhere, I'm just curious!
What you are trying to do is specifically designated as undefined behavior in the documentation:
The
longjmp()function restores the environment saved by the most recent invocation ofsetjmp()in the same thread, with the correspondingjmp_bufargument. If there is no such invocation, or if the function containing the invocation ofsetjmp()has terminated execution in the interim, the behaviour is undefined.
Since the function that called setjmp (i.e. funcB) has exited before you call longjmp in funcA, the behavior is undefined (it crashes on ideone).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With