Hi all I have a question about setjmp and longjmp
suppose i have this code
Code:
#include <setjmp.h>
jmp_buf buf1,buf2;
void f2(){
if(!setjmp(buf2)){
longjmp(buf1,1);
}
}
void f1(){
if(!setjmp(buf1)){
f2();
}
int x=0;
if(!x){
x=1;
longjmp(buf2,1); //error happens here
}
}
int main(){
f1();
return 0;
}
This code has an error that when f1 called f2 then setjmp(buf2) made buf2 points to the stack position but when longjmp(buf1,1) is called it means f2 returned so it's position in stack is now garbage and the behavior of longjmp ( buf2,1) is now unpredictable .
the solution to that is to save the entire stack and restoring it when returning to f2 again
The question is how this can be done !!!???