7_13:验证longjmp后变量的值

来源:互联网 发布:神优化游戏 编辑:程序博客网 时间:2024/06/05 22:30

一源代码:

     1  #include "apue.h"
     2  #include <setjmp.h>
     3
     4  void f1(int,int,int,int);
     5  void f2(void);
     6
     7  static jmp_buf jmpbuffer;
     8  static int globval;
     9
    10  int main()
    11  {
    12          int             autoval;
    13          register int    regival;
    14          volatile int    volaval;
    15          static int      statval;
    16
    17          globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5;
    18
    19          if( setjmp(jmpbuffer) != 0 ){
    20                  printf("after longjmp,the value is:\n");
    21                  printf("globval = %d\n",globval);
    22                  printf("autoval = %d\n",autoval);
    23                  printf("regival = %d\n",regival);
    24                  printf("volaval = %d\n",volaval);
    25                  printf("statval = %d\n",statval);
    26
    27                  exit(0);
    28          }
    29
    30          globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99;
    31          f1(autoval, regival, volaval, statval);
    32          exit(0);
    33  }
    34
    35
    36  void f1(int autoval, int regival, int volaval,int statval)
    37  {
    38          printf("In the f1 function,the values is:\n");
    39          printf("autoval = %d\n",autoval);
    40          printf("regival = %d\n",regival);
    41          printf("volaval = %d\n",volaval);
    42          printf("statval = %d\n",statval);
    43          f2();
    44  }
    45
    46  void f2()
    47  {
    48          longjmp(jmpbuffer,1);
    49  }
    50
    51

2 运行结果:

a. 不进行优化:

gcc -Wall -ggdb3 -O -o longjmp_value_O 7_13.c
In file included from apue.h:132,
                 from 7_13.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'




./longjmp_value
In the f1 function,the values is:
autoval = 96
regival = 97
volaval = 98
statval = 99
after longjmp,the value is:
globval = 95
autoval = 96
regival = 97
volaval = 98
statval = 99

b 优化后

gcc -Wall -ggdb3 -O -o longjmp_value_O 7_13.c
In file included from apue.h:132,
                 from 7_13.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'



./longjmp_value_O
In the f1 function,the values is:
autoval = 96
regival = 97
volaval = 98
statval = 99
after longjmp,the value is:
globval = 95
autoval = 2
regival = 3
volaval = 98
statval = 99




0 0