Setjmp and longjmp usage in C

Source: Internet
Author: User

Setjmp and longjmp usage in C
C provides the goto syntax. You can use goto to jump to a line of code marked inside the function body, but it cannot jump out of any position outside the function.
To solve this problem, the C function library provides setjmp () and longjmp () functions, which are used for non-local location record labels and goto functions.

To use these two functions, You need to include the header file.

 

int setjmp(jmp_buf env)
Create a local jmp_buf buffer and initialize it for future jump back here. This subroutine stores the call environment of the program in the buffer zone referred to by the env parameter, and env will be used by longjmp. If it is returned directly from the setjmp call, the setjmp return value is 0. If the call environment is returned for a program recovered from longjmp, setjmp returns a non-zero value.

 

 

void longjmp(jmp_buf env, int value)
Restore the context of the Program Calling environment in the buffer zone referred to by env. The content of the buffer zone referred to by env is saved by the setjmp subprogram call. The value of value is passed from longjmp to setjmp. After longjmp is completed, the program continues execution from the corresponding setjmp call, just as the setjmp call has just been completed. If the value is passed to longjmp, The setjmp return value is 1; otherwise, the setjmp return value is value.

 

When longjmp is used, the content of j is destroyed.
Longjmp can only be used to jump back to a previous place. There is still a process activity record at the place where setjmp is executed. From this perspective, longjmp is more like "Where to come from" than "where to go ". In addition, longjmp accepts an extra integer parameter and returns its value. It can be seen whether longjmp is transferred here or it is executed from the execution of the previous statement.

The following demo demonstrates the use of setjmp and longjmp.

 

//// Main. c // setjmp & longjmp /// Created by apple on 15/1/28. // Copyright (c) 2015 cc. all rights reserved. // # include
 
  
# Include
  
   
// Record the recovered location variable jmp_buf buf; void test () {printf (entering the test () function); // jump to the buf location variable record location longjmp (buf, 1 ); printf (return the test () function);} int main (int argc, const char * argv []) {// setjmp (buf) if it is returned in the setjmp function, returns 0 // if it is returned through the longjmp function, it returns a non-0 value if (setjmp (buf) {// it returns a non-0 value, jump to the setjump function through the longjmp function and return printf (main ()... setjump () function returned through longjmp);} else {// when setjmp is called for the first time, it must be returned after normal execution, that is, 0 value printf (main ()... setjump () function returns after normal execution); test ();} return 0 ;}
  
 
The output is as follows.

 

The printed result shows that the longjmp function does jump back to the line of code in the function of the Record Point. Setjmp and longjmp are generally used for exception handling. When an exception occurs, they jump out to the mark point. The try catch throw implementation in C ++ is the encapsulation based on this, which is the same as goto, using longjmp for redirection breaks the sequential execution structure of the program and makes it difficult to understand and debug the code. Therefore, this method is not recommended in C ++ to handle exceptions, in some advanced ides, Code after using the longjump function is marked as warning. For example, the XCode I use will prompt the Code will never be executed, the code will never be executed.

 

 

 

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.