C + + setjmp and longjmp cannot handle C + + exceptions (8)---"more effective C + +" __c++

Source: Internet
Author: User

SETJMP and longjmp non-localized jump statements: Nonlocal refers to, this is not similar to the normal C language goto statement, the statement in a function to jump, but on the stack to jump, may skip many call frames, and then return to the current function call path in a function.

Use setjmp and longjmp where you need attention:
1) setjmp and longjmp are used in combination, have their corresponding call order, setjmp before, longjmp in order to be able to revert to the previously saved "program execution point", if the order is reversed will make the execution of the program unpredictable, very easy to make the program crash exit;
2) longjmp not only need to setjmp after the call, but also must be in the scope of the setjmp, in fact, the internal implementation of this mechanism is this: thesetjmp function is to save the local environment of the invocation in a JMP_BUF memory structure, Because the corresponding memory in the calling function is not released, until the function returns, the local memory will be invalidated, then the call to longjmp can be restored to the setjmp place according to the JMP_BUF parameter and then to the next statement of the setjmp execution, Note that when setjmp first runs, the return value is 0.

Now let's take a look at the following examples to understand the setjmp and longjmp operating mechanisms:

#include <iostream>
#include <string>
#include <csetjmp>
using namespace std;
Static jmp_buf buf;

Void Weiwei () {
    cout << ' weiwei,you ' re the only one girl that I have seen who's eyes have all of the world I want to live! " <<endl;
    LONGJMP (buf, 1);
}
void Love () {
    Weiwei ();
    cout << "I love U, no matter how long I wait for you" << Endl;
}
int main () {
    int i = 0;
    SETJMP (BUF);
    Love ();
    cout << Endl;
    return 0;
}

Run Result:

It can be judged that the longjmp call does jump to the setjmp after the execution of the statement, because our program is written in the dead loop, so will continue to die cycle.

Now, let's see why we can't use setjmp and longjmp for exception handling in C + +. Let's take a look at the following code:
1 Add the Class object before longjmp the same function scope:

#include <iostream>
#include <string>
#include <csetjmp>
using namespace std;

Class myweiwei{
private:
    string name;
Public:
    Myweiwei () {

    }
    Myweiwei (string name): Name (name) {
        cout << "I am:" << name << " , but also hope that everyone a lot of attention. "<< Endl;
    }
    void Show () {
        cout << name << endl;
    }
    ~myweiwei () {
        cout << "First off the line, eat to go ~ ~ ~" << Endl;
    }
;
Jmp_buf buf;
Void Weiwei () {
    Myweiwei Myweiwei ("Now charge the sister");
    LONGJMP (buf, 1);
}
void Love () {
    Weiwei ();
}
int main () {
    int i=setjmp (BUF);
    if (i = = 0) {love
        ();
    }
    cout << "Then I wait for you online ... "<< Endl;
    return 0;
}

Run Result:

2) and the first case is equivalent to another form:

#include <iostream>
#include <string>
#include <csetjmp>
using namespace std;

Class myweiwei{
private:
    string name;
Public:
    Myweiwei () {

    }
    Myweiwei (string name): Name (name) {
        cout << "I am:" << name << " , but also hope that everyone a lot of attention. "<< Endl;
    }
    void Show () {
        cout << name << endl;
    }
    ~myweiwei () {
        cout << "First off the line, eat to go ~ ~ ~" << Endl;
    }
;
Jmp_buf buf;
Void Weiwei () {
    longjmp (buf, 1);
}
void Love () {
    Myweiwei Myweiwei ("Now charged with learning sister");
    Weiwei ();
}
int main () {
    int i=setjmp (BUF);
    if (i = = 0) {
        //love ();
        Myweiwei Myweiwei ("Now charge study sister");
        LONGJMP (buf,1);
    }
    cout << "Then I wait for you online ... "<< Endl;
    return 0;
}

Run Result:

3) is not within the scope of the same function as longjmp:

#include <iostream>
#include <string>
#include <csetjmp>
using namespace std;

Class myweiwei{
private:
    string name;
Public:
    Myweiwei () {

    }
    Myweiwei (string name): Name (name) {
        cout << "I am:" << name << " , but also hope that everyone a lot of attention. "<< Endl;
    }
    void Show () {
        cout << name << endl;
    }
    ~myweiwei () {
        cout << "First off the line, eat to go ~ ~ ~" << Endl;
    }
;
Jmp_buf buf;
Void Weiwei () {
    longjmp (buf, 1);
}
void Love () {
    Myweiwei Myweiwei ("Now charged with learning sister");
    Weiwei ();
}
int main () {
    int i = setjmp (BUF);
    if (i = = 0) {love
        ();
    }
    else{
        cout<<i<<endl;
    }
    cout << "Then I wait for you online ... "<< Endl;
    return 0;
}

Run Result:

With these three comparisons, we can see that it is true that setjmp and longjmp are not appropriate to handle C + + exceptions because they sometimes make it impossible for the destructor of an object created by C + + to execute.

PS:
int setjmp (jmp_buf env);/return value, if the direct call returns 0, returns the Val value in longjmp from the longjmp call;
void longjmp (Jmp_buf env,int val); /Call longjmp returns to the place where setjmp is located, executes the SETJMP's last statement, and the Val value becomes the setjmp return value, which is shown in the above procedure (3), because there can be multiple setjmp for a longjmp statement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.