I am trying to create an infinite loop using those functions (unsuccessfully).
My code :
jmp_buf buf1;
void foo(){
int z = 4, y = 1;
int v = setjmp(buf1);
if(v == 0){
printf("A%d", z);
longJmp(buf1, 1);
}
else if(v == 1){
printf("B%d", y);
longjmp(buf1,1);
}
}
int main(){
int v = setjmp(buf1);
if( v == 0){
printf("C%d", 1);
foo();
longjump(buf1,1);
}
}
I thought it would print C1A4B1B1B1.... (B1 repeats forever) but I just get C1A4B1 and the program stops (SEGMENTATION ERROR). Isn't calling longJmp always return to setJmp with parameter val?
Would like to understand where is my mistake.
EDIT : The code I copied before I got answers is mistakenly incorrect, longjmp(buf1,2) instead of longjmp(buf1,1). Also I didn't copy the full main code which cause the seg fault. (another longjmp after foo being called)