\hypertarget{group__setjmp}{}\doxysubsection{$<$setjmp.\+h$>$\+: Non-\/local goto}
\label{group__setjmp}\index{$<$setjmp.h$>$: Non-\/local goto@{$<$setjmp.h$>$: Non-\/local goto}}
\doxysubsubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
int \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp}} (jmp\+\_\+buf \+\_\+\+\_\+jmpb)
\item 
void \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp}} (jmp\+\_\+buf \+\_\+\+\_\+jmpb, int \+\_\+\+\_\+ret) \+\_\+\+\_\+\+A\+T\+T\+R\+\_\+\+N\+O\+R\+E\+T\+U\+R\+N\+\_\+\+\_\+
\end{DoxyCompactItemize}


\doxysubsubsection{Detailed Description}
While the C language has the dreaded {\ttfamily goto} statement, it can only be used to jump to a label in the same (local) function. In order to jump directly to another (non-\/local) function, the C library provides the \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} and \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} functions. \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} and \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} are useful for dealing with errors and interrupts encountered in a low-\/level subroutine of a program. 

\begin{DoxyNote}{Note}
\mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} and \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} make programs hard to understand and maintain. If possible, an alternative should be used.

\mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} can destroy changes made to global register variables (see \mbox{\hyperlink{FAQ_faq_regbind}{How to permanently bind a variable to a register?}}).
\end{DoxyNote}
For a very detailed discussion of \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}}/longjmp(), see Chapter 7 of {\itshape Advanced Programming in the U\+N\+IX Environment}, by W. Richard Stevens.

Example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{setjmp_8h}{setjmp.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{jmp\_buf env;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int} main (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{    \textcolor{keywordflow}{if} (\mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp}} (env))}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        ... handle error ...}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keywordflow}{while} (1)}
\DoxyCodeLine{    \{}
\DoxyCodeLine{       ... main processing loop which calls foo() some where ...}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{...}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void} foo (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{    ... blah, blah, blah ...}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keywordflow}{if} (err)}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp}} (env, 1);}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\}}
\end{DoxyCode}
 

\doxysubsubsection{Function Documentation}
\mbox{\Hypertarget{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}\label{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}} 
\index{$<$setjmp.h$>$: Non-\/local goto@{$<$setjmp.h$>$: Non-\/local goto}!longjmp@{longjmp}}
\index{longjmp@{longjmp}!$<$setjmp.h$>$: Non-\/local goto@{$<$setjmp.h$>$: Non-\/local goto}}
\doxyparagraph{\texorpdfstring{longjmp()}{longjmp()}}
{\footnotesize\ttfamily void longjmp (\begin{DoxyParamCaption}\item[{jmp\+\_\+buf}]{\+\_\+\+\_\+jmpb,  }\item[{int}]{\+\_\+\+\_\+ret }\end{DoxyParamCaption})}



Non-\/local jump to a saved stack context. 


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{setjmp_8h}{setjmp.h}}>}}
\end{DoxyCode}


\mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} restores the environment saved by the last call of \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} with the corresponding {\itshape \+\_\+\+\_\+jmpb} argument. After \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} is completed, program execution continues as if the corresponding call of \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} had just returned the value {\itshape \+\_\+\+\_\+ret}.

\begin{DoxyNote}{Note}
\mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} cannot cause 0 to be returned. If \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} is invoked with a second argument of 0, 1 will be returned instead.
\end{DoxyNote}

\begin{DoxyParams}{Parameters}
{\em \+\_\+\+\_\+jmpb} & Information saved by a previous call to \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}}. \\
\hline
{\em \+\_\+\+\_\+ret} & Value to return to the caller of \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}}.\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
This function never returns. 
\end{DoxyReturn}
\mbox{\Hypertarget{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}\label{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}} 
\index{$<$setjmp.h$>$: Non-\/local goto@{$<$setjmp.h$>$: Non-\/local goto}!setjmp@{setjmp}}
\index{setjmp@{setjmp}!$<$setjmp.h$>$: Non-\/local goto@{$<$setjmp.h$>$: Non-\/local goto}}
\doxyparagraph{\texorpdfstring{setjmp()}{setjmp()}}
{\footnotesize\ttfamily int setjmp (\begin{DoxyParamCaption}\item[{jmp\+\_\+buf}]{\+\_\+\+\_\+jmpb }\end{DoxyParamCaption})}



Save stack context for non-\/local goto. 


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{setjmp_8h}{setjmp.h}}>}}
\end{DoxyCode}


\mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} saves the stack context/environment in {\itshape \+\_\+\+\_\+jmpb} for later use by \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}}. The stack context will be invalidated if the function which called \mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} returns.


\begin{DoxyParams}{Parameters}
{\em \+\_\+\+\_\+jmpb} & Variable of type {\ttfamily jmp\+\_\+buf} which holds the stack information such that the environment can be restored.\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
\mbox{\hyperlink{group__setjmp_ga2687c5ef7a3f376db90908999a9a7fc6}{setjmp()}} returns 0 if returning directly, and non-\/zero when returning from \mbox{\hyperlink{group__setjmp_ga87f44eafaab5ec0ef8f5a11a8b853acf}{longjmp()}} using the saved context. 
\end{DoxyReturn}
