Keil Logo

longjmp

Summary
#include <setjmp.h>

void longjmp (
  jmp_buf env,      /* environment to restore */
  int retval);      /* return value */
Description

The longjmp function restores the state which was previously stored in env by the setjmp function. The retval argument specifies the value to return from the setjmp function.

The longjmp and setjmp functions may be used to execute a non-local goto. They are usually utilized to pass control to an error recovery routine.

Local variables and function arguments are restored only if declared with the volatile attribute.

Return Value

None.

See Also

setjmp

Example
#include <setjmp.h>
#include <stdio.h> /* for printf */

jmp_buf env; /* jump environment (must be global) */
bit error_flag;

void trigger (void) {
  .
  .
  .
  /* processing code here */
  .
  .
  .
  if (error_flag != 0) {
    longjmp (env, 1);   /* return 1 to setjmp */
  }
  .
  .
  .
}


void recover (void) {
  /* recovery code here */
}


void tst_longjmp (void) {
  .
  .
  .
  if (setjmp (env) != 0) {     /* setjmp returns a 0 */
    printf ("LONGJMP called\n");
    recover ();
  }

  else {
    printf ("SETJMP called\n");

    error_flag = 1;            /* force an error */

    trigger ();
  }
}
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.