static 在C的用法理解

本文通过一个具体的C++示例,详细解析了静态变量的概念及其在函数中的行为特点,并通过汇编代码进一步说明静态变量的存储机制。

static variable in a function

:

1.Allocated once at  a fixed address,that is , the object is created in a special static data area rather than  on the stack each time a function is called.This is the concept of static storage.

 

#include <iostream>
using namespace std;

int test(int a)
{
 static int i=3;//第一次调用函数时,初始化.第二次调用函数时,此句不执行
 
 i++;
 return i;
}
int main()
{
 cout<<test(3)<<endl;
 cout<<test(3)<<endl;
}

输出结果:4      5

  1. --- F:/1/file1.cpp  ------------------------------------------------------------------------------------------------------
  2. 1:    #include <iostream>
  3. 2:    using namespace std;
  4. 3:
  5. 4:    int test(int a)
  6. 5:    {
  7. 00401030   push        ebp
  8. 00401031   mov         ebp,esp
  9. 00401033   sub         esp,40h
  10. 00401036   push        ebx
  11. 00401037   push        esi
  12. 00401038   push        edi
  13. 00401039   lea         edi,[ebp-40h]
  14. 0040103C   mov         ecx,10h
  15. 00401041   mov         eax,0CCCCCCCCh
  16. 00401046   rep stos    dword ptr [edi]
  17. 6:        static int i=3;//注意这句没相应的汇编,变量存在静态存储区
  18. 7:
  19. 8:        i++;
  20. 00401048   mov         eax,[___xt_z+110h (00434dc0)]
  21. //内存跟踪   00434DC0  03 00 00 00
  22. 0040104D   add         eax,1
  23. 00401050   mov         [___xt_z+110h (00434dc0)],eax
  24. 9:        return i;
  25. 00401055   mov         eax,[___xt_z+110h (00434dc0)]
  26. 10:   }
  27. 0040105A   pop         edi
  28. 0040105B   pop         esi
  29. 0040105C   pop         ebx
  30. 0040105D   mov         esp,ebp
  31. 0040105F   pop         ebp
  32. 00401060   ret
  33. --- F:/1/file1.cpp  ------------------------------------------------------------------------------------------------------
  34. 11:   int main()
  35. 12:   {
  36. 00401070   push        ebp
  37. 00401071   mov         ebp,esp
  38. 00401073   sub         esp,40h
  39. 00401076   push        ebx
  40. 00401077   push        esi
  41. 00401078   push        edi
  42. 00401079   lea         edi,[ebp-40h]
  43. 0040107C   mov         ecx,10h
  44. 00401081   mov         eax,0CCCCCCCCh
  45. 00401086   rep stos    dword ptr [edi]
  46. 13:       test(3);
  47. 00401088   push        3
  48. 0040108A   call        @ILT+0(test) (00401005)
  49. 0040108F   add         esp,4   //由push   3这条指令引起,堆栈平衡
  50. 14:       test(3);
  51. 00401092   push        3
  52. 00401094   call        @ILT+0(test) (00401005)
  53. 00401099   add         esp,4
  54. 15:   }
  55. 0040109C   pop         edi
  56. 0040109D   pop         esi
  57. 0040109E   pop         ebx
  58. 0040109F   add         esp,40h
  59. 004010A2   cmp         ebp,esp//?这两句的作用是?
  60. 004010A4   call        __chkesp (00408190)//?
  61. 004010A9   mov         esp,ebp
  62. 004010AB   pop         ebp
  63. 004010AC   ret

2.File static

Local to a particular translation unit (and local to a class scope in C++,as you will see later),Here ,static controls the visibility of a name.So the name can not be seen outside the translation unit or class.This also describes the concept of linkage,which determines what names the linker will see.

这个我无法从汇编中看出区别.希望高手赐教.

 

网上找到的call _chkesp(******)的资料

调试版才会有,检查栈是否被破坏了.这样就能及时在你出错的地方停下了告诉你.发行版自动消除这些代码(调试产生的).有时候也可以手工加入这些调试检查代码,用于检查并确定错误所在位置。

 

 

关闭/GZ开关,可以去掉

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值