Skip to content

Instantly share code, notes, and snippets.

@jagt
Created March 31, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagt/9895365 to your computer and use it in GitHub Desktop.
Save jagt/9895365 to your computer and use it in GitHub Desktop.
setjmp/longjmp and volatile
#include <setjmp.h>
#include <stdio.h>
jmp_buf buf;
void jmper() {
// only variables marked as volatile can be seen as reiable
// written after jmp, as they are written to ram
volatile int local = 1;
if (!setjmp(buf)) {
printf("pre set: %d\n", local); // 1
local = 2;
printf("post set: %d\n", local); // 2
longjmp(buf, 1);
} else {
printf("post jmp: %d\n", local); // 2
}
}
int main() {
jmper();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment