longjmp setjmp and volatile

longjmp setjmp and volatile

1 /******************************************************************************* 2 * 版权所有: 3 * 模 块 名: 4 * 文 件 名:volatile_setjmp.c 5 * 实现功能: 6 * 作 者:XYZ 7 * 版 本:V1.0 8 * 日 期:2013.11.15 9 * 联系方式:xiao13149920@foxmail.com#include<stdio.h>13 #include<stdlib.h>14 #include<setjmp.h> jmp_buf env;do_jump(int nvar, int rvar, int vvar)19 {, nvar, rvar, vvar);21longjmp(env, 1);22 }main(int argc, char *argv[])25 {26int nvar;vvar; nvar = 111;31rvar = 222;32vvar = 333;(setjmp(env) == 0)35 {36nvar = 777;37rvar = 888;38vvar = 999;39 do_jump(nvar, rvar, vvar);40 } {, nvar, rvar, vvar);44 };47 }

1. When we compile the program with

gcc -o volatile_setjmp volatile_setjmp.c

and the output :

Inside do_jump(): nvar:777, rvar:888, vvar:999After do_jump(): nvar:777, rvar:888, vvar:999

2.However, when we compile with optimization

gcc -O -o volatile_setjmp volatile_setjmp.

we get the following unexpected results:

Inside do_jump(): nvar:777, rvar:888, vvar:999After do_jump(): nvar:111, rvar:222, vvar:999

We can prevent such code reorganization by declaring variables as volatile,which tells the optimizer not to optimizethem. In the preceding program output,we see that the variable vvar, which was declared volatile, was correctly handled,even when we compiled with optimization.

posted on

,可是却依旧为对方擦去嘴角的油渍。

longjmp setjmp and volatile

相关文章:

你感兴趣的文章:

标签云: