 | Cx51 User's Guide |  |
|
|
| setjmp| Summary |
#include <setjmp.h>
int setjmp (
jmp_buf env); /* current environment */
| | Description | The setjmp function saves the current state of the CPU in env. The state may be restored by a subsequent call to the longjmp function. When used together, the setjmp and longjmp functions provide you with a way to execute a non-local goto. A call to the setjmp function saves the current instruction address as well as other CPU registers. A subsequent call to the longjmp function restores the instruction pointer and registers, and execution resumes at the point just after the setjmp call. Local variables and function arguments are restored only if declared with the volatile attribute. | | Return Value | The setjmp function returns a value of 0 when the current state of the CPU has been copied to env. A non-zero value indicates that the longjmp function was executed to return to the setjmp function call. In such a case, the return value is the value passed to the longjmp function. | | See Also | longjmp | | Example |
#include <setjmp.h>
#include <stdio.h> /* for printf */
jmp_buf env; /* jump environment (must be global) */
bit error_flag;
void trigger (void) {
.
.
.
/* processing code here */
.
.
.
if (error_flag != 0) {
longjmp (env, 1); /* return 1 to setjmp */
}
.
.
.
}
void recover (void) {
/* recovery code here */
}
void tst_longjmp (void) {
.
.
.
if (setjmp (env) != 0) { /* setjmp returns a 0 */
printf ("LONGJMP called\n");
recover ();
}
else {
printf ("SETJMP called\n");
error_flag = 1; /* force an error */
trigger ();
}
}
|
Related Knowledgebase Articles |
|