Borland C++ reference

longjmp

raw OCR

Performs nonlocal goto.

Defined in header <setjmp.h>

Syntax

#include <setjmp.h>
void longjmpCjmpbuf    /mp&, int retval);

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

A call to longjmp restores the task state captured by the last call to setjmp with the argument jmpb. It then returns in such a way that setjmp appears to have returned with the value retval.

A task state is

B all segment registers (CS, DS, ES, SS) B register variables (SI, DI) ■ stack pointer (SP) B frame base pointer (BP) B flags

A task state is complete enough that setjmp and longjmp can be used to implement coroutines.

setjmp must be called before longjmp. The routine that called setjmp and set up jmpb must still be active and cannot have returned before the longjmp is called. If this happens, the results are unpredictable.

longjmp cannot pass the value 0; if 0 is passed in retval, longjmp will substitute 1.

You can't use setjmp and longjmp for implementing coroutines if your program is overlaid. Normally, setjmp and longjmp save and restore all the registers needed for coroutines, but the overlay manager needs to keep track of stack contents and assumes there is only one stack. When you implement coroutines there are usually either two stacks or two partitions of one stack, and the overlay manager will not track them properly.

You can have background tasks which run with their own stacks or sections of stack, but you must ensure that the background tasks do not invoke any overlaid code, and you must not use the overlay versions of setjmp or longjmp to switch to and from background.

Return value

None.

See also

Example

#include <stdio.h>
tt'include <setjmp.h>
#include <stdlib.h>

void subroutine! jmp_buf);

int main (void)
{
   int value;
   jinp_buf jumper;
   value  = setjmp(jumper);
   if  (value 1= 0) {
      printf ("Longjmp with value %d\n", value)
      exit (value);

   printf ("About to call subroutine ... \n");
   subroutine (jumper);
   return  0;
}
void subroutine (jmp_buf jumper) {
   longjmp (jumper,1);
}

Program    output

About to call  subroutine
Longjmp with value  1

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.