Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 2.21 KB

using-setjmp-longjmp.md

File metadata and controls

28 lines (19 loc) · 2.21 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: Using setjmp and longjmp
Using setjmp and longjmp
08/14/2018
longjmp_cpp
setjmp_cpp
C++ exception handling, setjmp/longjmp functions
setjmpex.h
longjmp function in C++ programs
setjmp.h
setjmp function
setjmp function, C++ programs
96be8816-f6f4-4567-9a9c-0c3c720e37c5

Using setjmp and longjmp

When setjmp and longjmp are used together, they provide a way to execute a non-local goto. They are typically used in C code to pass execution control to error-handling or recovery code in a previously called routine without using the standard calling or return conventions.

Caution

Because setjmp and longjmp don't support correct destruction of stack frame objects portably between C++ compilers, and because they might degrade performance by preventing optimization on local variables, we don't recommend their use in C++ programs. We recommend you use try and catch constructs instead.

If you decide to use setjmp and longjmp in a C++ program, also include <setjmp.h> or <setjmpex.h> to assure correct interaction between the functions and Structured Exception Handling (SEH) or C++ exception handling.

Microsoft Specific

If you use an /EH option to compile C++ code, destructors for local objects are called during the stack unwind. However, if you use /EHs or /EHsc to compile, and one of your functions that uses noexcept calls longjmp, then the destructor unwind for that function might not occur, depending on the optimizer state.

In portable code, when a longjmp call is executed, correct destruction of frame-based objects is explicitly not guaranteed by the standard, and may not be supported by other compilers. To let you know, at warning level 4, a call to setjmp causes warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable.

END Microsoft Specific

See also

Mixing C (Structured) and C++ Exceptions