From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/program%2Flongjmp.html | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 devdocs/c/program%2Flongjmp.html (limited to 'devdocs/c/program%2Flongjmp.html') diff --git a/devdocs/c/program%2Flongjmp.html b/devdocs/c/program%2Flongjmp.html new file mode 100644 index 00000000..2ccde1dd --- /dev/null +++ b/devdocs/c/program%2Flongjmp.html @@ -0,0 +1,64 @@ +

longjmp

Defined in header <setjmp.h>
void longjmp( jmp_buf env, int status );
+
(until C11)
_Noreturn void longjmp( jmp_buf env, int status );
+
(since C11)
(until C23)
[[noreturn]] void longjmp( jmp_buf env, int status );
+
(since C23)

Loads the execution context env saved by a previous call to setjmp. This function does not return. Control is transferred to the call site of the macro setjmp that set up env. That setjmp then returns the value, passed as the status.

+

If the function that called setjmp has exited (whether by return or by a different longjmp higher up the stack), the behavior is undefined. In other words, only long jumps up the call stack are allowed.

+ + +

Jumping across threads (if the function that called setjmp was executed by another thread) is also undefined behavior.

+
(since C11)
+ +

If when setjmp was called, a VLA or another variably-modified type variable was in scope and control left that scope, longjmp to that setjmp invokes undefined behavior even if control remained within the function.

+

On the way up the stack, longjmp does not deallocate any VLAs, memory leaks may occur if their lifetimes are terminated in this way:

+
void g(int n)
+{
+    int a[n]; // a may remain allocated
+    h(n); // does not return
+}
+void h(int n)
+{
+    int b[n]; // b may remain allocated
+    longjmp(buf, 2); // might cause a memory leak for h's b and g's a
+}
(since C99)

Parameters

+ + +
env - variable referring to the execution state of the program saved by setjmp
status - the value to return from setjmp. If it is equal to ​0​, 1 is used instead

Return value

(none)

+

Notes

longjmp is intended for handling unexpected error conditions where the function cannot return meaningfully. This is similar to exception handling in other programming languages.

+

Example

#include <stdio.h>
+#include <setjmp.h>
+#include <stdnoreturn.h>
+ 
+jmp_buf my_jump_buffer;
+ 
+noreturn void foo(int status) 
+{
+    printf("foo(%d) called\n", status);
+    longjmp(my_jump_buffer, status + 1); // will return status+1 out of setjmp
+}
+ 
+int main(void)
+{
+    volatile int count = 0; // modified local vars in setjmp scope must be volatile
+    if (setjmp(my_jump_buffer) != 5) // compare against constant in an if
+        foo(++count);
+}

Output:

+
foo(1) called
+foo(2) called
+foo(3) called
+foo(4) called

References

See also

+ +
saves the context
(function macro)
C++ documentation for longjmp
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/program/longjmp +

+
-- cgit v1.2.3