zoukankan      html  css  js  c++  java
  • C Standard Library: 8 Nonlocal Jumps: <setjmp.h>

    The declarations in <setjmp.h> provide a way to avoid the normal function call and return
    sequence, typically to permit an immediate return from a deeply nested function call.
    int setjmp(jmp_buf env)
    The macro setjmp saves state information in env for use by longjmp. The return is
    zero from a direct call of setjmp, and non-zero from a subsequent call of longjmp. A
    call to setjmp can only occur in certain contexts, basically the test of if, switch, and
    loops, and only in simple relational expressions.
    if (setjmp(env) == 0)
    /* get here on direct call */
    else
    /* get here by calling longjmp */
    void longjmp(jmp_buf env, int val)
    longjmp restores the state saved by the most recent call to setjmp, using the
    information saved in env, and execution resumes as if the setjmp function had just
    executed and returned the non-zero value val. The function containing the setjmp
    must not have terminated. Accessible objects have the values they had at the time
    longjmp was called, except that non-volatile automatic variables in the function
    calling setjmp become undefined if they were changed after the setjmp call.

     setjmp和longjmp函数使用详解

    非局部跳转语句---setjmp和longjmp函数。非局部指的是,这不是由普通C语言goto,语句在一个函数内实施的跳转,而是在栈上跳过若干调用帧,返回到当前函数调用路径上的某一个函数中。
    #include <setjmp.h>
    Int setjmp(jmp_buf  env);
       返回值:若直接调用则返回0,若从longjmp调用返回则返回非0值
    Void longjmp(jmp_buf env,int val);
        在希望返回到的位置调用setjmp,此位置在main函数中,因为直接调用该函数,所以其返回值为0.setjmp参数evn的类型是一个特殊的类型jmp_buf,这一数据类型是某种形式的数组,其中存放在调用longjmp时能用来恢复栈状态的所有信息。因为需要在另一个函数中引用env变量,所以规范的处理方式是将env变量定义为全局变量。
       当检查到一个错误时,则以两个参数调用longjmp函数,第一个就是在调用setjmp时所用的env,第二个参数是具有非0值的val,它将成为从setjmp处返回的值。使用第二个参数的原因是对于一个setjmp可以有多个longjmp。
        下面我们可以看一个简单的例子

    #include <stdio.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <string.h>
    
    void fun1(void);
    void fun2(void);
     jmp_buf jmpbuffer;
    void main(void)
    {
        int i = 0;
        int j = 0;
        i = setjmp(jmpbuffer);
        if(i==0)
        {
            printf("first run/n");
            fun1();
            fun2();
        }
       else
       {
         switch(i)
         {
           
         case 1:
           printf("In fun1 /n");
         break;
       case 2:
         printf("In fun2/n");
         break;
       default:
         printf("unkown error/n");
         break;
         }
              exit(0);
       }
        return 1;
    } 
    
    void fun1(void)
    {
        char *s = "hello";
        char *s1 = "Hello";
        if(strcmp(s,s1)!=0)
        longjmp(jmpbuffer,1);
    }
    
    void fun2(void)
    {
        char *s = "world";
        if(strcmp(s,"World")!=0)
        longjmp(jmpbuffer,2);
    }
    root@root:~/program/test_program$ ./jmp_test
    first run
    In fun1

    这个函数的运行结果是:

       在使用longjmp跳转到setjmp中时,程序主动的退出了!相当于抛出一个异常退出!其实这两个函数可以模拟C++中的异常函数:
    使用setjmp和longjmp要注意以下几点:
      1、setjmp与longjmp结合使用时,它们必须有严格的先后执行顺序,也即先调用setjmp函数,之后再调用longjmp函数,以恢复到先前被保存的“程序执行点”。否则,如果在setjmp调用之前,执行longjmp函数,将导致程序的执行流变的不可预测,很容易导致程序崩溃而退出
     2、不要假设寄存器类型的变量将总会保持不变。在调用longjmp之后,通过setjmp所返回的控制流中,程序中寄存器类型的变量将不会被恢复。寄存器类型的变量,是指为了提高程序的运行效率,变量不被保存在内存中,而是直接被保存在寄存器中。寄存器类型的变量一般都是临时变量,在C语言中,通过register定义,或直接嵌入汇编代码的程序。这种类型的变量。
         longjmp必须在setjmp调用之后,而且longjmp必须在setjmp的作用域之内。具体来说,在一个函数中使用setjmp来初始化一个全局标号,然后只要该函数未曾返回,那么在其它任何地方都可以通过longjmp调用来跳转到 setjmp的下一条语句执行。实际上setjmp函数将发生调用处的局部环境保存在了一个jmp_buf的结构当中,只要主调函数中对应的内存未曾释放 (函数返回时局部内存就失效了),那么在调用longjmp的时候就可以根据已保存的jmp_buf参数恢复到setjmp的地方执行。

  • 相关阅读:
    驱动调试常见问题_Camera
    如何从零开始开发一款嵌入式产品(20年的嵌入式经验)
    git入门与实践
    ramfs, rootfs, initrd and initramfs
    Living a Fulfilling Life: A Guide to Following Your Heart
    有关 jffs2_scan_eraseblock 问题小结
    JS_imgload
    JS_闭包和内存.
    JS_textarea自适应高度
    Css_制作旋转字体
  • 原文地址:https://www.cnblogs.com/freewater/p/2973424.html
Copyright © 2011-2022 走看看