static修饰局部变量 首先来看一段程序,思考一下该程序调用foo、bar函数后输出a的值分别会是多少呢? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio.h> int main(){ foo(); bar(); foo(); bar(); return 0; } void foo() { static int a = 0x12345678; ++a; printf("a in foo(), location: 0x%x, current value: 0x%x\n", &a, a); } void bar() { static int a = 0x87654321; ++a; printf("a in bar(), location: 0x%x, current value: 0x%x\n", &a, a); } 使用gcc编译上面的程序,运行程序,控制台输出如下图 从程序的输出我们可以看到foo和bar中的a的存储地址不同,而且每次调用foo和bar,它们自己的变量a的值都增加了1。再来看一下它们的反汇编代码 可以看到a并不在栈上。使用objdump -h Test.……

阅读全文