Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of using long jumps in C to jump interprocedurally
#include <stdio.h>
#include <setjmp.h>
// saves the stack context/environment for nonlocal gotos
jmp_buf env;
// foo immediately makes a long jump to the setjmp in main
void foo(void){
longjmp(env,1);
}
// prints "hello cruel world!"
int main(int argc, char *argv[]){
printf("hello");
// setjmp returns 0 if returning directly and nonzero
// when returning from a longjmp or siglongjmp using
// the saved stack context
if (!setjmp(env)){
foo();
printf(" sane world!\n");
return 0;
} else {
printf(" cruel world!\n");
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.