C library function -longjmp()

C Standard Library - <setjmp.h>

Description

C Library Functionsvoid longjmp(jmp_buf environment, int value)Restore the environment saved by the most recent callsetjmp()to the environment saved when the macro was invoked,jmp_bufThe parameter setting is generated by a previous call to setjmp().

longjmp()is the C standard library<setjmp.h>a function used for non-local jumps. It allows the program to jump between multiple frames on the stack, typically used for error handling or exception handling, as well as for control flow changes in certain specific situations.

Declaration

The following is the declaration of the longjmp() function.

#include <setjmp.h>

void longjmp(jmp_buf env, int val);

Parameter

  • env: onejmp_bufa variable of type, which storessetjmp()return information.
  • val: an integer value, assetjmp()the return value of.

Return Value

The longjmp() function itself has no return value, but it transfers program control flow to the location where setjmp() was previously called, and makes val the return value of setjmp().

Example

The following example demonstrates the usage of the longjmp() function.

Example

#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

void second(void) {
    printf("second\n");         // Print
    longjmp(buf,1);             // Jump back to the setjmp call site - making setjmp return 1
}

void first(void) {
    second();
    printf("first\n");          // It is impossible to execute this line.
}

int main() {  
    if ( ! setjmp(buf) ) {
        first();                // Before entering this line, setjmp returns 0
    } else {                    // When longjmp jumps back, setjmp returns 1, so this line is entered.
        printf("main\n");       // Print
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

second
main

Notes

  • Usagelongjmp()andsetjmp()They need to be used with caution, because they can make code difficult to understand and debug, especially in large programs.
  • longjmp()They may bypass routine resource cleanup and destructor calls, which can lead to resource leaks and undefined behavior.
  • They should be avoided in environments involving multi-threading or signal handlers.longjmp()andsetjmp()。

Use Case

  • Error handling and exception handling:longjmp()andsetjmp()They can be used to implement simple error and exception handling mechanisms.
  • State machines: During state transitions in a state machine, it is sometimes necessary to jump between different states.longjmp()andsetjmp()can be used to implement this type of state transition.
  • Used together with signal handling: In some special cases,longjmp()andsetjmp()they can be used to jump out of signal handlers.

Summary

longjmp()andsetjmp()They provide a very flexible mechanism for jumping between different parts of a program, but they must be used with great care to avoid potential problems and undefined behavior. They are typically used for error handling and exception handling, as well as for control flow changes in certain specific situations.

C Standard Library - <setjmp.h>

other extensions