You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Restores the stack environment and execution locale set by a `setjmp` call.
21
21
22
22
## Syntax
23
23
@@ -30,27 +30,39 @@ void longjmp(
30
30
31
31
### Parameters
32
32
33
-
*env*
33
+
*env*
34
34
Variable in which environment is stored.
35
35
36
-
*value*
37
-
Value to be returned to **setjmp** call.
36
+
*value*
37
+
Value to be returned to `setjmp` call.
38
38
39
39
## Remarks
40
40
41
-
The **longjmp** function restores a stack environment and execution locale previously saved in *env* by **setjmp**. **setjmp** and **longjmp** provide a way to execute a nonlocal **goto**; they are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal call and return conventions.
41
+
The **longjmp** function restores a stack environment and execution locale previously saved in *env* by `setjmp`. `setjmp` and **longjmp** provide a way to execute a nonlocal **goto**; they are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal call and return conventions.
42
42
43
-
A call to **setjmp** causes the current stack environment to be saved in *env*. A subsequent call to **longjmp** restores the saved environment and returns control to the point immediately following the corresponding **setjmp** call. Execution resumes as if *value* had just been returned by the **setjmp** call. The values of all variables (except register variables) that are accessible to the routine receiving control contain the values they had when **longjmp** was called. The values of register variables are unpredictable. The value returned by **setjmp** must be nonzero. If *value* is passed as 0, the value 1 is substituted in the actual return.
43
+
A call to `setjmp` causes the current stack environment to be saved in *env*. A subsequent call to **longjmp** restores the saved environment and returns control to the point immediately following the corresponding `setjmp` call. Execution resumes as if *value* had just been returned by the `setjmp` call. The values of all variables (except register variables) that are accessible to the routine receiving control contain the values they had when **longjmp** was called. The values of register variables are unpredictable. The value returned by `setjmp` must be nonzero. If *value* is passed as 0, the value 1 is substituted in the actual return.
44
44
45
-
Call **longjmp** before the function that called **setjmp** returns; otherwise the results are unpredictable.
45
+
**Microsoft Specific**
46
+
47
+
In Microsoft C++ code on Windows, **longjmp** uses the same stack-unwinding semantics as exception-handling code. It is safe to use in the same places that C++ exceptions can be raised. However, this usage is not portable, and comes with some important caveats.
48
+
49
+
Only call **longjmp** before the function that called `setjmp` returns; otherwise the results are unpredictable.
46
50
47
51
Observe the following restrictions when using **longjmp**:
48
52
49
-
- Do not assume that the values of the register variables will remain the same. The values of register variables in the routine calling **setjmp** may not be restored to the proper values after **longjmp** is executed.
53
+
- Do not assume that the values of the register variables will remain the same. The values of register variables in the routine calling `setjmp` may not be restored to the proper values after **longjmp** is executed.
54
+
55
+
- Do not use **longjmp** to transfer control out of an interrupt-handling routine unless the interrupt is caused by a floating-point exception. In this case, a program may return from an interrupt handler via **longjmp** if it first reinitializes the floating-point math package by calling [_fpreset](fpreset.md).
56
+
57
+
- Do not use **longjmp** to transfer control from a callback routine invoked directly or indirectly by Windows code.
50
58
51
-
-Do not use **longjmp** to transfer control out of an interrupt-handling routine unless the interrupt is caused by a floating-point exception. In this case, a program may return from an interrupt handler via **longjmp**if it first reinitializes the floating-point math package by calling **_fpreset**.
59
+
-If the code is compiled by using **/EHs** or **/EHsc** and the function that contains the **longjmp** call is **noexcept**then local objects in that function may not be destructed during the stack unwind.
52
60
53
-
**Note** Be careful when using **setjmp** and **longjmp** in C++ programs. Because these functions do not support C++ object semantics, it is safer to use the C++ exception-handling mechanism.
61
+
**END Microsoft Specific**
62
+
63
+
> [!NOTE]
64
+
> In portable C++ code, you can't assume `setjmp` and `longjmp` support C++ object semantics. Specifically, a `setjmp`/`longjmp` call pair has undefined behavior if replacing the `setjmp` and `longjmp` by **catch**
65
+
and **throw** would invoke any non-trivial destructors for any automatic objects. In C++ programs, we recommend you use the C++ exception-handling mechanism.
54
66
55
67
For more information, see [Using setjmp and longjmp](../../cpp/using-setjmp-longjmp.md).
56
68
@@ -62,15 +74,11 @@ For more information, see [Using setjmp and longjmp](../../cpp/using-setjmp-long
62
74
63
75
For additional compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
64
76
65
-
## Libraries
66
-
67
-
All versions of the [C run-time libraries](../../c-runtime-library/crt-library-features.md).
68
-
69
77
## Example
70
78
71
79
See the example for [_fpreset](fpreset.md).
72
80
73
81
## See also
74
82
75
-
[Process and Environment Control](../../c-runtime-library/process-and-environment-control.md)<br/>
76
-
[setjmp](setjmp.md)<br/>
83
+
[Process and Environment Control](../../c-runtime-library/process-and-environment-control.md)
Copy file name to clipboardExpand all lines: docs/c-runtime-library/reference/setjmp.md
+16-9Lines changed: 16 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "setjmp | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "08/14/2018"
5
5
ms.technology: ["cpp-standard-libraries"]
6
6
ms.topic: "reference"
7
7
apiname: ["setjmp"]
@@ -29,22 +29,30 @@ int setjmp(
29
29
30
30
### Parameters
31
31
32
-
*env*<br/>
32
+
*env*
33
33
Variable in which environment is stored.
34
34
35
35
## Return Value
36
36
37
-
Returns 0 after saving the stack environment. If **setjmp** returns as a result of a **longjmp** call, it returns the **value** argument of **longjmp**, or if the **value** argument of **longjmp** is 0, **setjmp** returns 1. There is no error return.
37
+
Returns 0 after saving the stack environment. If **setjmp** returns as a result of a `longjmp` call, it returns the *value* argument of `longjmp`, or if the *value* argument of `longjmp` is 0, **setjmp** returns 1. There is no error return.
38
38
39
39
## Remarks
40
40
41
-
The **setjmp** function saves a stack environment, which you can subsequently restore, using **longjmp**. When used together, **setjmp** and **longjmp** provide a way to execute a non-local **goto**. They are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal calling or return conventions.
41
+
The **setjmp** function saves a stack environment, which you can subsequently restore, using `longjmp`. When used together, **setjmp** and `longjmp` provide a way to execute a non-local **goto**. They are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal calling or return conventions.
42
42
43
-
A call to **setjmp** saves the current stack environment in *env*. A subsequent call to **longjmp** restores the saved environment and returns control to the point just after the corresponding **setjmp** call. All variables (except register variables) accessible to the routine receiving control contain the values they had when **longjmp** was called.
43
+
A call to **setjmp** saves the current stack environment in *env*. A subsequent call to `longjmp` restores the saved environment and returns control to the point just after the corresponding **setjmp** call. All variables (except register variables) accessible to the routine receiving control contain the values they had when `longjmp` was called.
44
44
45
45
It is not possible to use **setjmp** to jump from native to managed code.
46
46
47
-
**Note****setjmp** and **longjmp** do not support C++ object semantics. In C++ programs, use the C++ exception-handling mechanism.
47
+
**Microsoft Specific**
48
+
49
+
In Microsoft C++ code on Windows, **longjmp** uses the same stack-unwinding semantics as exception-handling code. It is safe to use in the same places that C++ exceptions can be raised. However, this usage is not portable, and comes with some important caveats. For details, see [longjmp](longjmp.md).
50
+
51
+
**END Microsoft Specific**
52
+
53
+
> [!NOTE]
54
+
> In portable C++ code, you can't assume `setjmp` and `longjmp` support C++ object semantics. Specifically, a `setjmp`/`longjmp` call pair has undefined behavior if replacing the `setjmp` and `longjmp` by **catch**
55
+
and **throw** would invoke any non-trivial destructors for any automatic objects. In C++ programs, we recommend you use the C++ exception-handling mechanism.
48
56
49
57
For more information, see [Using setjmp and longjmp](../../cpp/using-setjmp-longjmp.md).
50
58
@@ -62,6 +70,5 @@ See the example for [_fpreset](fpreset.md).
62
70
63
71
## See also
64
72
65
-
[Process and Environment Control](../../c-runtime-library/process-and-environment-control.md)<br/>
0 commit comments