-

Functions HI-TECH C LONGJMP

From HI-TECH C for CP/M Fan WIKI(EN)
Revision as of 22:20, 31 July 2017 by Kumokosi (talk | contribs) (Created page with "<strong>LONGJMP</strong> ==SYNOPSIS== #include <setjmp.h> void longjmp(jmp_buf buf, int val) ==DESCRIPTION== <strong>Longjmp</strong>(), in conjunction with <str...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

LONGJMP

SYNOPSIS

#include <setjmp.h>

void      longjmp(jmp_buf buf, int val)


DESCRIPTION

Longjmp(), in conjunction with setjmp(), provides a mechanism for non-local gotos. To use this facility, setjmp() should be called with a jmp_buf argument in some outer level function. The call from setjmp() will return 0. To return to this level of execution, lonjmp() may be called with the same jmp_buf argument from an inner level of execution. Note however that the function which called setjmp() must still be active when longjmp() is called. Breach of this rule will cause disaster, due to the use of a stack containing invalid data. The val argument to longjmp() will be the value apparently returned from the setjmp(). This should normally be non-zero, to distinguish it from the genuine setjmp() call. For example:

#include <setjmp.h>

static jmp_buf  jb_err;

main()
{
    if(setjmp(jb_err)) {
    printf("An error occured0);
    exit(1);
    }
    a_func();
}

a_func()
{
    if(do_whatever() != 0)
    longjmp(jb_err, 1);
    if(do_something_else() != 0)
    longjmp(jb_err, 2);
}


The calls to longjmp() above will never return; rather the call to setjmp() will appear to return, but with a return value equal to the argument supplied to longjmp().

SEE ALSO

setjmp