This blog is under construction

Saturday, 5 May 2012

setjmp, longjmp

Header file:

     int setjmp(jmp_buf env);
     void longjmp(jmp_buf env, int val);

 setjmp It saves the stack environment in  env for use by longjmp.  And the stack environment gets invalidated if the function calling setjmp returns.
 longjmp It restores the state saved by the most recent call of setjmp or it makes our program execution shift to the last call of setjmp.

Sample program for setjmp and longjmp:

  int main() {
        jmp_buf env;
        int flag = 0;
        if(setjmp(env) == 0) {
                printf("setjmp called directly\n");
        } else {
                printf("Hit due to the call of longjmp\n");
                flag = 1;
        printf("End of code\n");
        return 0;

  jp@jp-VirtualBox:~/cpgms/setjmp$ ./a.out
  setjmp called directly
  Hit due to the call of longjmp
  End of code

No comments:

Post a Comment