zoukankan      html  css  js  c++  java
  • C语言之setjmp

    #include <stdio.h>
    #include <stdlib.h>
    #include <setjmp.h>
    
    static jmp_buf buf;
    
    int main(void)
    {
       volatile int b = 3;
    
       if (setjmp(buf) != 0)
       {
          printf("%d\n", b);
          exit(0);
       }
       b = 5;
       longjmp(buf, 1);
       
       return 0;
    }
    

    Answer: 5

    The setjmp function stores context information for a “non-local goto”, and returns 0.  Thelongjmp function transfers control to the setjmp call that initializedbuf, and execution continues from this point as if setjmp had returned 1.

    Note: a non-volatile automatic variable that has been modified after setjmp becomes indeterminate afterlongjmp.  Without the volatile qualifier, this program’s behavior would be undefined.  This rule permits better optimization of code.

    关键点在于理解setjmp以及longjmp,(http://en.wikipedia.org/wiki/Setjmp.h )第一次运行到setjmp,会设置jmp_buf,然后返回0。当调用longjmp时,会把longjmp里面的非0值作为setjmp的返回值返回(如果longjmp的value参数为0,setjmp恢复后返回1,也就是当恢复到setjmp存储点的时候,setjmp一定不会返回0)。——Veda原型

  • 相关阅读:
    常见的灰度发布系统规则
    golang中的路由分组
    艾森豪威尔矩阵
    列文定理
    吃狗粮定理
    mysql事务 锁
    mysql中explain优化分析
    mysql hash索引优化
    各种浏览器内核介绍
    浏览器 兼容性问题总结
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007646.html
Copyright © 2011-2022 走看看