Skip to content

Instantly share code, notes, and snippets.

@benjholla
Created February 10, 2016 18:28
Show Gist options
  • Save benjholla/28db25b1f7ffac4eb98c to your computer and use it in GitHub Desktop.
Save benjholla/28db25b1f7ffac4eb98c to your computer and use it in GitHub Desktop.
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