Name

setjmp, longjmp, _setjmp, _longjmp
- save stack context for non-local goto

Library

libc.lib

Synopsis

  #include <setjmp.h>
  int setjmp (jmp_buf env);
  void longjmp (jmp_buf env, int val);
  int _setjmp (jmp_buf env);
  void _longjmp (jmp_buf env, int val);

Return values

If the return is from a direct invocation, the setjmp function returns 0. If the return is from a call to longjmp(), setjmp() returns a non-zero value. After the longjmp is completed, program execution continues as if the corresponding invocation of setjmp() had just returned the value specified by val. If val is 0, setjmp() returns 1.

Detailed description

The setjmp, and _setjmp functions save their calling environment in env. Each of these functions returns 0.

The corresponding longjmp functions restore the environment saved by their most recent respective invocations of the setjmp function. They then return so that program execution continues as if the corresponding invocation of the setjmp call had just returned the value specified by val, instead of 0.

The longjmp routines may not be called after the routine which called the setjmp routines returns.

All accessible objects have values as of the time longjmp routine was called, except that the values of objects of automatic storage invocation duration that do not have the volatile type and have been changed between the setjmp invocation and longjmp call are indeterminate.

The setjmp / longjmp pairs save and restore the signal mask while _setjmp / _longjmp pairs save and restore only the register set and the stack.


Examples

#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
  
static void f1( int, int, int );
static void f2( void );
static jmp_buf jmpbuffer;
  
int main()
{
        int count;
        register int val;
        volatile int sum;
  
        count = 2; val = 3; sum = 4;
  
        if ( setjmp( jmpbuffer ) != 0 )
        {
    printf("in main: count = %d, val = %d, sum = %d\n", count, val, sum );
    exit(0);
  }
  
        f1(97, 98, 99 );
  
        return 0;
}
   
static void f1 (int i, int j, int k )
{
          printf("in f1: count = %d, val = %d, sum = %d\n", i, j , k );
    f2();
}
   
static void f2( void )
{
    longjmp( jmpbuffer, 1 );
}


Output

in f1: count = 97, val = 98, sum = 99
in main: count = 2, val = 3, sum = 4


Errors

No errors are defined.

Limitations

The signal related functionalities aren’t applicable to the symbian implementation as there is no support for signals from symbian. This essentially means that the setjmp and _setjmp routines have the same functionality and so is the case with longjmp and _longjmp routines.

Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top