标签:
转贴,原文地址:http://www.cnblogs.com/lq0729/archive/2011/10/23/2222117.html
1 #include<iostream> 2 #include<setjmp.h> 3 #include<stdlib.h> 4 #include<stdio.h> 5 6 using namespace std; 7 8 jmp_buf jmpbuffer; 9 10 void g() 11 { 12 cout << "in g()" << endl; 13 14 longjmp(jmpbuffer, 2); 15 } 16 17 void f() 18 { 19 cout << "in f()" << endl; 20 21 g(); 22 23 cout << "leave f()" << endl; 24 } 25 26 int globval; 27 28 int main() 29 { 30 int autoval; 31 register int regival; 32 volatile int volaval; 33 static int statval; 34 35 cout << "begin" << endl; 36 37 globval = 90; 38 autoval = 91; 39 regival = 92; 40 volaval = 93; 41 statval = 94; 42 43 int i = setjmp(jmpbuffer); 44 45 cout << "setjmp return code: " << i << endl; 46 47 if(2 == i) 48 { 49 cout << "error code: "<< i << endl; 50 51 cout << "globval= " << globval << "; "; 52 cout << "autoval= " << autoval << "; "; 53 cout << "regival= " << regival << "; "; 54 cout << "volaval= " << volaval << "; "; 55 cout << "statval= " << statval << "; " << endl; 56 return 0; 57 } 58 59 globval = 0; 60 autoval = 1; 61 regival = 2; 62 volaval = 3; 63 statval = 4; 64 65 cout << "globval= " << globval << "; "; 66 cout << "autoval= " << autoval << "; "; 67 cout << "regival= " << regival << "; "; 68 cout << "volaval= " << volaval << "; "; 69 cout << "statval= " << statval << "; " << endl; 70 71 f(); 72 73 return 0; 74 }
使用O3优化编译,结果如下:
在非优化时,包括寄存器变量(register修饰)在内的所有类型都被存放在内存中,所以所有变量都是更改后的值。
在优化编译时,自动变量(默认类型)和寄存器变量被存储在寄存器中,其它变量与非优化时一样。所以autoval和regival都还原为原来的值。
附jmp_buf结构:
typedef struct
{
unsigned j_sp; // 堆栈指针寄存器
unsigned j_ss; // 堆栈段
unsigned j_flag; // 标志寄存器
unsigned j_cs; // 代码段
unsigned j_ip; // 指令指针寄存器
unsigned j_bp; // 基址指针
unsigned j_di; // 目的指针
unsigned j_es; // 附加段
unsigned j_si; // 源变址
unsigned j_ds; // 数据段
} jmp_buf;
标签:
原文地址:http://www.cnblogs.com/ysmintor/p/4356775.html