常量 栈 自由存储区 堆 全局/静态存储区

 在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。
    栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区。里面的变量通常是局部变量、函数参数等。
    堆,就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去控制,一般一个new就要对应一个delete。如果程序员没有释放掉,那么在程序结束后,操作系统会自动回收。
    自由存储区,就是那些由malloc等分配的内存块,他和堆是十分相似的,不过它是用free来结束自己的生命的。
    全局/静态存储区,全局变量和静态变量被分配到同一块内存中,在以前的C语言中,全局变量又分为初始化的和未初始化的,在C++里面没有这个区分了,他们共同占用同一块内存区。
    常量存储区,这是一块比较特殊的存储区,他们里面存放的是常量,不允许修改(当然,你要通过非正当手段也可以修改,而且方法很多)

Const   Data             The   const   data   area   stores   string   literals   and  
                                    other   data   whose   values   are   known   at   compile  
                                    time.     No   objects   of   class   type   can   exist   in  
                                    this   area.     All   data   in   this   area   is   available  
                                    during   the   entire   lifetime   of   the   program.  
   
                                    Further,   all   of   this   data   is   read-only,   and   the  
                                    results   of   trying   to   modify   it   are   undefined.  
                                    This   is   in   part   because   even   the   underlying  
                                    storage   format   is   subject   to   arbitrary  
                                    optimization   by   the   implementation.     For  
                                    example,   a   particular   compiler   may   store   string  
                                    literals   in   overlapping   objects   if   it   wants   to.  
   
   
    Stack                       The   stack   stores   automatic   variables.   Typically  
                                    allocation   is   much   faster   than   for   dynamic  
                                    storage   (heap   or   free   store)   because   a   memory  
                                    allocation   involves   only   pointer   increment  
                                    rather   than   more   complex   management.     Objects  
                                    are   constructed   immediately   after   memory   is  
                                    allocated   and   destroyed   immediately   before  
                                    memory   is   deallocated,   so   there   is   no  
                                    opportunity   for   programmers   to   directly  
                                    manipulate   allocated   but   uninitialized   stack  
                                    space   (barring   willful   tampering   using   explicit  
                                    dtors   and   placement   new).  
   
   
    Free   Store             The   free   store   is   one   of   the   two   dynamic   memory  
                                    areas,   allocated/freed   by   new/delete.     Object  
                                    lifetime   can   be   less   than   the   time   the   storage  
                                    is   allocated;   that   is,   free   store   objects   can  
                                    have   memory   allocated   without   being   immediately  
                                    initialized,   and   can   be   destroyed   without   the  
                                    memory   being   immediately   deallocated.     During  
                                    the   period   when   the   storage   is   allocated   but  
                                    outside   the   object's   lifetime,   the   storage   may  
                                    be   accessed   and   manipulated   through   a   void*   but  
                                    none   of   the   proto-object's   nonstatic   members   or  
                                    member   functions   may   be   accessed,   have   their  
                                    addresses   taken,   or   be   otherwise   manipulated.  
   
   
    Heap                         The   heap   is   the   other   dynamic   memory   area,  
                                    allocated/freed   by   malloc/free   and   their  
                                    variants.     Note   that   while   the   default   global  
                                    new   and   delete   might   be   implemented   in   terms   of  
                                    malloc   and   free   by   a   particular   compiler,   the  
                                    heap   is   not   the   same   as   free   store   and   memory  
                                    allocated   in   one   area   cannot   be   safely  
                                    deallocated   in   the   other.   Memory   allocated   from  
                                    the   heap   can   be   used   for   objects   of   class   type  
                                    by   placement-new   construction   and   explicit  
                                    destruction.     If   so   used,   the   notes   about   free  
                                    store   object   lifetime   apply   similarly   here.  
   
   
    Global/Static       Global   or   static   variables   and   objects   have  
                                    their   storage   allocated   at   program   startup,   but  
                                    may   not   be   initialized   until   after   the   program  
                                    has   begun   executing.     For   instance,   a   static  
                                    variable   in   a   function   is   initialized   only   the  
                                    first   time   program   execution   passes   through   its  
                                    definition.     The   order   of   initialization   of  
                                    global   variables   across   translation   units   is   not  
                                    defined,   and   special   care   is   needed   to   manage  
                                    dependencies   between   global   objects   (including  
                                    class   statics).     As   always,   uninitialized   proto-  
                                    objects'   storage   may   be   accessed   and   manipulated  
                                    through   a   void*   but   no   nonstatic   members   or  
                                    member   functions   may   be   used   or   referenced  
                                    outside   the   object's   actual   lifetime. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值