The setjmp and longjmp in C

Source: Internet
Author: User

setjmp and longjmp belong to the C language, of course, C + + will have these two functions. Their prototype is as follows:

int setjmp (jmp_buf env);

function: The first time the helper, the register of the current state information is stored in the Env, and return 0. If longjmp (env,x) is called somewhere, and X!=0, the return value of setjmp is set to X. And if x==0, then setjmp returns 1.

void longjmp (jmp_buf env,int value);

Function: Re-stores the status information of the current register and sets the return value of setjmp to value.

Simply summarize, if you have a moonlight box, then you need to set a moonlight box somewhere in a point of time, such as November 11, 2015 11:11, later, I feel uncomfortable in 2016, directly using the Moonlight box back to November 11, 2015 11:11, At that time all things remained unchanged, only I knew I had traversed. Then setjmp is used to set the point of time, will be required to record the things, save in a jmp_buf type of variable, after that, if there is longjmp want to return to this point of time, you can jump back directly. It is worth noting that this is not always in the infinite loop? Of course there is a change, the first time you call setjmp, the return is 0. When the call longjmp jumps back, the return value of setjmp becomes a non-0 value, so it can be distinguished.

Here's an example:

1 voidTest ()2 {3 jmp_buf JB;4     intRET=SETJMP (JB);//sets the return point, which returns 0.5     if(ret!=0) cout<<"123"<<endl;//This is only output 1 times. 6     if(ret==0) longjmp (JB,7);//go back to JB and set the RET to non-zero. 7}

  

So what is the scope of setjmp and longjmp in C + +? SETJMP and longjmp If you jump out of the current function, the object declared in the current function is not to be destroyed (not to call the destructor), then there is a certain danger, such as the object and the dynamic application of memory what to do, but there is no such problem in C. It's not that you can't use it, you just have to understand its pros and cons to get better at it.

Example:

1 classRainbow2 {3  Public:4Rainbow () {cout<<"Rainbow ()"<<Endl;}5~rainbow () {cout<<"~rainbow ()"<<Endl;}6 };7 jmp_buf jmpbuf;8 voidSummer ()9 {Ten Rainbow RB; One      for(intI=3;i>0;--i) Acout<<"Happy Chinese Valentine ' s day!"<<Endl; -LONGJMP (Jmpbuf,2); -      the     //The following sentence is ignored -cout<<"Oh No. I am being ignore."<<Endl; - } -  + intMain () - {   + test (); A     if(!setjmp (jmpbuf)) at     { -cout<<"setjmp () return a zero value!"<<Endl; - Summer (); -     } -     Else -     { incout<<"setjmp () get a non-zero value!"<<Endl; -     } to     return 0;  +}

After testing, the destructor is not called under g++. Of course, you need to consider the level of intelligence of the compiler, which may help you optimize it.

This method looks similar to Goto, but it differs from the following:

(1) The Goto statement cannot jump out of the current function of the C language.

(2) Use longjmp can only jump back to the place you have been. There is still a process activity record in place where the setjmp is executed. From this perspective, longjmp is more like "where to come from" than "where to go". In addition, LONGJMP accepts an additional shaping parameter and returns its value, which can be known whether it is transferred from longjmp to here or from the previous statement, and is executed naturally here.

Reference: http://blog.csdn.net/cscmaker/article/details/7584433

  

The setjmp and longjmp in C

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.