Created
March 31, 2014 15:48
-
-
Save jagt/9895365 to your computer and use it in GitHub Desktop.
setjmp/longjmp and volatile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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