C Standard Library -
Introduction
setjmp.h The header file defines the macro setjmp(), the function longjmp() and the variable type jmp_buf, which bypasses the normal function call and return rules.
is a header file in the C Standard Library that provides a set of functions and macros for non-local jumps (i.e., jumping from one function to another function that was previously called, without using the normal function return mechanism). This mechanism is commonly used for error handling, exception handling, or for breaking out of deeply nested loops or recursion.
Library variables
The variables defined in the header file setjmp.h are listed below:
| No. | Variable & Description |
|---|---|
| 1 | **jmp_buf ** |
|
|
Library macros
The following is the only macro defined in this library:
| No. | Macro & description |
|---|---|
| 1 | int setjmp(jmp_buf environment) |
| This macro saves the current environment in the variable environment so that the function longjmp() can use it later. If this macro returns directly from the macro call, it returns zero, but if it returns from a longjmp() function call, it returns a non-zero value. |
Library functions
The following is the only function defined in the header file setjmp.h:
| No. | Function & Description |
|---|---|
| 1 | void longjmp(jmp_buf environment, int value) |
| This function restores the environment saved by the most recent call to setjmp() macro, and thejmp_buf parameters are set by a previous call to setjmp(). |
Example
The following example shows how to use setjmp() and longjmp() to implement a non-local jump:
Example
#include
#include
jmp_buf env
;
void
second
(
)
{
printf
(
"Entering second()\n"
)
;
longjmp
(
env
,
1
)
;
printf
(
"This line will never be executed\n"
)
;
}
void
first
(
)
{
printf
(
"Entering first()\n"
)
;
if
(
setjmp
(
env
)
==
0
)
{
printf
(
"Calling second() from first()\n"
)
;
second
(
)
;
}
else
{
printf
(
"Returned to first() from second() using longjmp\n"
)
;
}
printf
(
"Exiting first()\n"
)
;
}
int
main
(
)
{
printf
(
"Starting main()\n"
)
;
first
(
)
;
printf
(
"Exiting main()\n"
)
;
return
0
;
}
Running the above program will produce output similar to the following:
Starting main()
Entering first()
Calling second() from first()
Entering second()
Returned to first() from second() using longjmp
Exiting first()
Exiting main()
Code Analysis
jmp_buf env: Define a variable used to save environment informationenv.first()function: callsetjmp(env)to save the current environment information, and decide whether to callsecond()function in Vue 3.second()function: calllongjmp(env, 1), jump back tosetjmp(env), and cause it to return 1.main()function: callfirst()function in Vue 3.
Use Cases
- Error handling: Use in deeply nested function calls
setjmp()andlongjmp()to implement an error-handling mechanism. - Exception handling: Used to implement simple exception handling.
- Interrupting the control flow: In some cases, the normal control flow can be interrupted to break out of multiple levels of nested loops or function calls.
Notes
- Use
setjmp()andlongjmp()Care must be taken because they skip the normal stack unwinding process, which may lead to resource leaks (such as unreleased memory, unclosed files, etc.). - Avoid modifying local variables between calls to
setjmp()andlongjmp(), because this may lead to undefined behavior. - Non-local jumps can make code difficult to read and debug, so they should be used with caution.
Summary
provides setjmp() and longjmp() functions for implementing non-local jumps. These functions are very useful in error handling and exception handling, but must be used carefully to avoid potential problems.