Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

signal(2)

setjmp(3)

NAME

setjmp, longjmp − non-local goto

SYNTAX

#include <setjmp.h>

int setjmp (env)
jmp_buf env;

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

DESCRIPTION

The setjmp and longjmp functions help deal with errors and interrupts encountered in a low-level subroutine of a program.

The setjmp function saves its stack environment in env (whose type, jmp_buf, is defined in the <setjmp.h> header file) for later use by longjmp. It returns the value 0.

The longjmp function restores the environment saved by the last call of setjmp with the corresponding env argument.  After longjmp finishes, program execution continues as if the corresponding call of setjmp (which must not itself have returned in the interim) had just returned the value val.  The longjmp function cannot cause setjmp to return the value 0.  If longjmp is invoked with a second argument of 0, setjmp returns 1. At the time of the second return from setjmp, all accessible data have values as of the time longjmp is called. However, global variables have the expected values. For example, those as of the time of the longjmp(see

EXAMPLE

#include <setjmp.h>
 jmp_buf env;
int i = 0;
main ()
{
void exit();
 if(setjmp(env) != 0) {
(void) printf("value of i on 2nd return from setjmp: %d0, i);
exit(0);
}
(void) printf("value of i on 1st return from setjmp: %d0, i);
i = 1;
g();
/*NOTREACHED*/
}
 g()
{
longjmp(env, 1);
/*NOTREACHED*/
}

If the a.out resulting from this C language code is run, the output is as follows:

value of i on 1st return from setjmp:0
 value of i on 2nd return from setjmp:1

NOTE

Unexpected behavior occurs if longjmp is called without a previous call to setjmp, or when the last such call was in a function which has since returned.

RESTRICTIONS

The values of the registers on the second return from setjmp are register values at the time of the first call to setjmp, not those of the longjmp. Thus, variables in a given function can produce unexpected results in the presence of setjmp, depending on whether they are register or stack variables.

SEE ALSO

signal(2). 
 
 
 

Subroutines

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026