C Standard Library -<setjmp.h>
Introduction
setjmp.hThe header file defines macrossetjmp(), functionlongjmp()and variable typesjmp_buf, this variable type bypasses the normal function call and return rules.
<setjmp.h>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 requiring the normal function return mechanism). This mechanism is usually used for error handling, exception handling, or jumping out of deeply nested loops or recursion.
Library variables
The following lists the variables defined in the header file setjmp.h:
| Serial Number | Variables & Description |
|---|---|
| 1 | jmp_buf
|
Library macros
The following is the only macro defined in this library:
| Serial Number | Macros & Description |
|---|---|
| 1 | int setjmp(jmp_buf environment) This macro saves the current environment in the variableenvironmentso that functionslongjmp()for later use. If this macro returns directly from a macro call, it returns zero, but if it returns from alongjmp()function call, it returns a non-zero value. |
Library functions
The following is the only function defined in the header file setjmp.h:
| Serial Number | Functions & Description |
|---|---|
| 1 | void longjmp(jmp_buf environment, int value) This function restores the most recent callsetjmp()the environment saved when the macro was invoked,jmp_bufThe parameter setting is generated by a previous call to setjmp(). |
Example
The following example shows how to use setjmp() and longjmp() to implement non-local jumps:
Example
#include <setjmp.h>
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 output something like the following:
Starting main() Entering first() Calling second() from first() Entering second() Returned to first() from second() using longjmp Exiting first() Exiting main()
Code explanation
jmp_buf env: define a variable for saving environment informationenv。first()function: callsetjmp(env)save the current environment information, and decide whether to call based on its return valuesecond()function.second()function: calllongjmp(env, 1), jump back tosetjmp(env), and make it return 1.main()function: callfirst()function.
Use Case
- Error Handling: used in deeply nested function calls
setjmp()andlongjmp()to implement an error handling mechanism. - Exception Handling: used to implement simple exception handling.
- interrupt control flow: in certain cases, the normal control flow can be interrupted to break out of multiple levels of nested loops or function calls.
Notes
- Usage
setjmp()andlongjmp()Need to be careful, because they skip the normal stack unwinding process, which may lead to resource leaks (such as unreleased memory, unclosed files, etc.). - avoid when calling
setjmp()andlongjmp()between modifying local variables, because this may lead to undefined behavior. - Non-local jumps may make code difficult to read and debug, so they should be used with caution.
Summary
<setjmp.h>providessetjmp()andlongjmp()functions used to implement non-local jumps. These functions are very useful in error handling and exception handling, but need to be used with caution to avoid potential problems.