zoukankan      html  css  js  c++  java
  • const全局常量占了多少空间

        最近把我写过的一个很大的C++程序给朋友看,朋友说我用了太多的const常量,会消耗很多静态区内存空间,最好用define代替。说实话,我在C++的代码里看到define,就像在美女脸上看到一个大黑痣那样的难受,所以我不愿意这么改。但是话说回来,我的const,真的占用了很多静态空间吗?我做了两个实验,测试环境是bcc55,使用-S参数生成汇编代码。废话不多说,我把实验结果帖出来:
     测试 1:

    const int TESTA = 1;
    const int TESTB = 2;

    int main()
    {
     int a = TESTA;
     return 0;
    }

    汇编码:

     .386p
     ifdef ??version
     if ??version GT 500H
     .mmx
     endif
     endif
     model flat
     ifndef ??version
     ?debug macro
     endm
     endif
     ?debug S "2.cpp"
     ?debug T "2.cpp"
    _TEXT segment dword public use32 'CODE'
    _TEXT ends
    _DATA segment dword public use32 'DATA'
    _DATA ends
    _BSS segment dword public use32 'BSS'
    _BSS ends
    DGROUP group _BSS,_DATA
    _TEXT segment dword public use32 'CODE'
    _main segment virtual
    @_main proc near
    ?live16385@0:
     ; 
     ; int main()
     ; 
     push ebp
     mov ebp,esp
     ; 
     ; {
     ;  int a = TESTA;
     ;  return 0;
     ; 
    @1:
     xor eax,eax
     ; 
     ; }
     ; 
    @3:
    @2:
     pop ebp
     ret
    @_main endp
    _main ends
    _TEXT ends
    _TEXT segment dword public use32 'CODE'
    _TEXT ends
     ?debug D "2.cpp" 13353 40136
     end

    测试2:

    const int TESTA = 1;
    const int TESTB = 2;

    int main()
    {
     const int *a = &TESTA;
     return 0;
    }

    汇编码:

     .386p
     ifdef ??version
     if ??version GT 500H
     .mmx
     endif
     endif
     model flat
     ifndef ??version
     ?debug macro
     endm
     endif
     ?debug S "2.cpp"
     ?debug T "2.cpp"
    _TEXT segment dword public use32 'CODE'
    _TEXT ends
    _DATA segment dword public use32 'DATA'
    _DATA ends
    _BSS segment dword public use32 'BSS'
    _BSS ends
    DGROUP group _BSS,_DATA
    _TEXT segment dword public use32 'CODE'
    _main segment virtual
    @_main proc near
    ?live16385@0:
     ; 
     ; int main()
     ; 
     push ebp
     mov ebp,esp
     ; 
     ; {
     ;  const int *a = &TESTA;
     ;  return 0;
     ; 
    @1:
     xor eax,eax
     ; 
     ; }
     ; 
    @3:
    @2:
     pop ebp
     ret
    @_main endp
    _main ends
    _TEXT ends
    _DATA segment dword public use32 'DATA'
    _TESTA segment virtual
     align 2
    @_TESTA label dword
     dd 1
    _TESTA ends
    _DATA ends
    _TEXT segment dword public use32 'CODE'
    _TEXT ends
     ?debug D "2.cpp" 13353 40238
     end

        可以看到,作为常量使用的const,并不占用存储空间,这个和define的作用是一样的;而如果你试图对这个常量取地址操作,编译器才会给它分配空间。时间上,你没必要对常量通过地址改变,也没办法改变的,因为,常量在编译阶段已经被替换了。用const取代你的define吧!

  • 相关阅读:
    svn command line tag
    MDbg.exe(.NET Framework 命令行调试程序)
    Microsoft Web Deployment Tool
    sql server CI
    VS 2010 One Click Deployment Issue “Application Validation did not succeed. Unable to continue”
    mshtml
    大厂程序员站错队被架空,只拿着五折工资!苟活和离职,如何选择?
    揭秘!Windows 为什么会蓝屏?微软程序员竟说是这个原因...
    喂!千万别忘了这个C语言知识!(~0 == -1 问题)
    Linux 比 Windows 更好,谁反对?我有13个赞成理由
  • 原文地址:https://www.cnblogs.com/freeman/p/314073.html
Copyright © 2011-2022 走看看