C Standard Libraries C++

longjmp()

Declaration

void longjmp(jmp_buf qEnvironment, int qReturn);

Description

This function sets the stack environment and execution locale via the passsed-in argument, qEnvironment. The stack environment is typically set via a prior call to setjmp(). The argument qReturn is the value that will be returned the next time that setjmp() is called. The function longjmp() can be used to jump execution back to a prior setjmp() call by passing in the retrieved stack environment variable.

Example

#include <iostream>
#include <csetjmp>

int main() {
    using namespace std;

    int x = 0;
    jmp_buf qEnv;

    // Use setjmp() and longjmp() to create a loop
    int iRet = setjmp(qEnv);
    cout << "Return value   = " << iRet << endl;
    if (x < 10) {
        ++x;
        // This jumps back to setjmp()
        longjmp(qEnv, x);
    }

    return 0;
}

Output

longjmp() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.