Files
oldlinux-files/Ref-docs/c_lib_guide/2.8.html
2024-02-19 00:21:47 -05:00

137 lines
3.4 KiB
HTML

<html>
<head>
<title>
C Guide--2.8 setjmp.h
</title>
<!-- Changed by: eric huss, 12-Mar-1997 -->
</head>
<body text="#000000" bgcolor="#FFFFFF">
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="2.7.html">
<img src="left.gif" border=0>
Previous Section<br>
2.7 math.h</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.9.html">
Next Section
<img src="right.gif" border=0><br>
2.9 signal.h</a></td>
</tr>
</table>
</center>
<hr>
<h1> 2.8 setjmp.h</h1>
<p>
The setjmp header is used for controlling low-level calls and returns to and from
functions.
<p>
Macros:
<blockquote><code><b>
setjmp();
</b></code></blockquote>
Functions:
<blockquote><code><b>
longjmp();
</b></code></blockquote>
Variables:
<blockquote><code><b>
typedef jmp_buf
</b></code></blockquote>
<p>
<a name="variables"></a>
<h2>2.8.1 Variables and Definitions</h2>
<p>
The variable type <code><b>jmp_buf</b></code> is an array type used for holding information for <code><b>setjmp</b></code> and
<code><b>longjmp</b></code>.
<p>
<a name="setjmp"></a>
<h2>2.8.2 setjmp</h2>
<p>
Declaration:
<blockquote>
<code><b>int setjmp(jmp_buf</b></code><i> environment</i><code><b>);</b></code>
</blockquote>
Saves the environment into the variable <i>environment</i>. If a non-zero value is returned,
then this indicates that the point in the sourcecode was reached by a <b><code>longjmp</code></b>. Otherwise zero is
returned indicating the environment has been saved.
<p>
<a name="longjmp"></a>
<h2>2.8.3 longjmp</h2>
<p>
Declaration:
<blockquote>
<code><b>void longjmp(jmp_buf</b></code><i> environment</i><code><b>, int</b></code><i> value</i><code><b>);</b></code>
</blockquote>
Causes the environment to be restored from a <code><b>setjmp</b></code> call where the environment variable
had been saved. It causes execution to goto the <code><b>setjmp</b></code> location as if <code><b>setjmp</b></code> had returned the
value of the variable <i>value</i>. The variable <i>value</i> cannot be zero. However, if zero is passed, then
1 is replaced. If the function where <code><b>setjmp</b></code> was called has terminated, then the results are
undefined.
<p>
Example:
<blockquote><code><b><pre>
#include&lt;setjmp.h&gt;
#include&lt;stdio.h&gt;
void some_function(jmp_buf);
int main(void)
{
int value;
jmp_buf environment_buffer;
value=setjmp(environment_buffer);
if(value!=0)
{
printf("Reached this point from a longjmp with value=%d.\n",value);
exit(0);
}
printf("Calling function.\n");
some_function(environment_buffer);
return 0;
}
void some_function(jmp_buf env_buf)
{
longjmp(env_buf,5);
}
</pre></b></code></blockquote>
The output from this program should be:<br>
<pre>
Calling function.
Reached this point from a longjmp with value=5.
</pre>
<hr>
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="2.7.html">
<img src="left.gif" border=0>
Previous Section<br>
2.7 math.h</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.9.html">
Next Section
<img src="right.gif" border=0><br>
2.9 signal.h</a></td>
</tr>
</table>
</center>
</body>
</html>