Skip to content

Instantly share code, notes, and snippets.

@Dysta
Created October 16, 2018 10:04
Show Gist options
  • Save Dysta/853b97844f901e49351912ef119226a4 to your computer and use it in GitHub Desktop.
Save Dysta/853b97844f901e49351912ef119226a4 to your computer and use it in GitHub Desktop.
Introduction à setjmp et longjmp
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
volatile /* utilisation du mot clé volatile lorsqu'on compile en -O3 */ int i = 0;
jmp_buf b;
void f( void ) {
longjmp(b, ++i);
}
/* ne fonctionne pas car on ne peut pas déscendre dans la pile */
int g( void ) {
return setjmp(b);
}
int main(int argc, char const *argv[]) {
printf("Boucle jmp1\n");
if ( setjmp(b) < 10 ) {
printf("%d\n", i);
f();
}
i =0;
printf("Boucle jmp2\n");
if ( g() < 10 ) {
printf("%d\n", i);
longjmp(b, ++i);
}
printf("Boucle for \n");
for ( i = 0; i < 10; i++)
{
printf("%d\n", i);
}
return EXIT_SUCCESS;
}
@ArnaudQu
Copy link

Merci Dysta t le bro

@yamidevs
Copy link

bg comme tjr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment