RAD Studio
ContentsIndex
PreviousUpNext
longjmp

Header File 

setjmp.h  

Category 

Miscellaneous Routines 

Prototype 

void longjmp(jmp_buf jmpb, int retval); 

Description 

Performs nonlocal goto. 

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 Win32 task state includes:

  • Register variables
  • EBX, EDI, ESI
  • Stack pointer (ESP)
  • Frame pointer (EBP)
  • No segment registers are saved
  • Flags are not saved
A task state is complete enough that setjmp and longjmp can be used to implement co-routines. 

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. 

Return Value 

None. 

Example  

#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
void subroutine(jmp_buf);
int main(void)
{
    
    int value;
    jmp_buf jumper;
    value = setjmp(jumper);
    if (value != 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);
}

Portability

POSIX 
Win32 
ANSI C 
ANSI C++ 
+  
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!