Name
setjmp
Synopsis
Saves the calling environment as a long jump destination
#include <setjmp.h> intsetjmp
( jmp_bufenv
);
The setjmp()
macro saves
the current environment at the time of the call in a buffer
specified by its argument. The environment includes the stack, and
with it all variables that have automatic storage duration. Like the
setjmp()
macro itself, the
argument’s type, jmp_buf
, is
defined in the header file setjmp.h.
A later call to the longjmp()
function restores the saved
environment. As a result, the longjmp()
function does not return, but
instead causes execution to continue as if control had returned from
the setjmp()
. However, while the
original setjmp()
call always
returns 0, the apparent return value after longjmp()
is never equal to zero.
Because the execution environment saved may not include other
partial expressions, the return value of setjmp()
must not be used except in simple
conditional expressions, or in comparison to an integer constant
value. Furthermore, if any variables with automatic storage duration
in the function that called setjmp()
were modified after the setjmp()
call (and were not declared as
volatile
), then their values
after the longjmp()
call are
indeterminate.
Example
This example shows the complete contents of two source files
to illustrate how setjmp()
and
longjmp()
allow you to escape
from a function call.
#include <stdlib.h> #include <stdio.h> #include <setjmp.h> #include <errno.h> double calculate1( double x); // Functions defined double ...
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.