comparison pointer_longjump.c @ 1:6d11ed2a5bed

write pointer_longjump.c
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 12 Nov 2013 13:06:35 +0900
parents dee9711aeb06
children 76cd6ae48a1b
comparison
equal deleted inserted replaced
0:dee9711aeb06 1:6d11ed2a5bed
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <setjmp.h> 2 #include <setjmp.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 4
5 int retval; 5 __code code1(int n,void *__return,void *__enviroment,int *__ret_p){
6 __code code1(int n,void *__return,void *__enviroment){ 6 void(*ret)(int,void *,int *);
7 void(*ret)(int,void *);
8 printf("code1\n"); 7 printf("code1\n");
9 ret = (void(*)(int,void *))__return; 8 ret = (void(*)(int,void *,int *))__return;
10 ret(n,__enviroment); 9 ret(n,__enviroment,__ret_p);
11 } 10 }
12 11
13 void *return1 (int n,void* env){ 12 void *return1 (int n,void* env,int* __ret_p){
14 printf("return1\n"); 13 printf("return1\n");
15 retval = n; 14 *__ret_p = n;
16 longjmp(*(jmp_buf*)env,1); 15 longjmp(*(jmp_buf*)env,1);
17 } 16 }
18 17
19 int main1 (){ 18 int main1 (){
20 void *__return; 19 void *__return;
21 void *__enviroment; 20 void *__enviroment;
21 int *__ret_p;
22 printf("main1 entry\n"); 22 printf("main1 entry\n");
23 __enviroment = (void*)malloc(sizeof(jmp_buf)); 23 __enviroment = (void*)malloc(sizeof(jmp_buf));
24 __ret_p = (int*)malloc(sizeof(int));
24 if (setjmp(__enviroment)){ 25 if (setjmp(__enviroment)){
25 free(__enviroment); 26 free(__enviroment);
26 printf("main1 return\n"); 27 printf("main1 return\n");
27 return retval; 28 return *__ret_p;
28 } 29 }
29 __return = (void*)return1; 30 __return = (void*)return1;
30 goto code1(30,__return,__enviroment); 31 goto code1(30,__return,__enviroment,__ret_p);
31 return 0; 32 return 0;
32 } 33 }
33 34
34 int main (){ 35 int main (){
35 int n; 36 int n;