1.main函数
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <setjmp.h> static jmp_buf jmpbuf; static int globval = 1; static void func2(void) { longjmp(jmpbuf, 1); } static void func1(int i, int j, int k, int l) { fprintf(stdout, "in func1:\n"); fprintf(stdout, "globval = %d," "autoval = %d," "regival = %d," "volaval = %d," "statval = %d\n", globval, i, j, k , l); func2(); } int main(int argc, char* argv[]) { int autoval = 2; register int regival = 3; volatile int volaval = 4; static int statval = 5; if (setjmp(jmpbuf) != 0) { fprintf(stdout, "after longjmp:\n"); fprintf(stdout, "globval = %d," "autoval = %d," "regival = %d," "volaval = %d," "statval = %d\n", globval, autoval, regival, volaval, statval); exit(0); } globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99; func1(autoval, regival, volaval, statval); exit(0); }
编译时不进行优化,结果如下:
in func1: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99 after longjmp: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99
编译时进行全部优化,结果如下:
in func1: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99 after longjmp: globval = 95,autoval = 2,regival = 3,volaval = 98,statval = 99
我们可以看到:全局、静态、易失变量不受优化的影响,在调用longjmp之后,他们的值是最近所呈现的值
未优化时,所有5个变量都存放在存储器中,而进行优化之后,autoval 和 regival 都存放在寄存器中,而 volatile变量仍存放在 存储器中
对于 volatile 修饰符用法,这里有一篇很好的文章:http://hedengcheng.com/?p=725
APUE 学习笔记(五) 进程环境,布布扣,bubuko.com
原文:http://www.cnblogs.com/wwwjieo0/p/3714484.html