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).

  • 栈区:
    栈区存储自动变量,特别是栈区分配内存的速度比动态内存分配快的多(不管是在堆区还是在自由存储区)。这是因为栈区分配内存只需要指针增加一下就好了,而动态内存分配需要复杂的内存分配管理(查找空闲链表,做标记,划分,合并等)。在栈区内,为对象分配内存后,会立即调用该对象的构造函数进行初始化,对象销毁后立即释放该对象所占用的内存。所以根本没有给程序员直接管理栈区内存(除了未初始化的)的机会。当就像你知道的规则天生就有例外,用 explicit dtors(显式析构函数)和 placement new(定位 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 啦。自由存储区的内存由 new/delete 进行申请和释放。对象的生命周期可以比该对象持有的内存的生命期更短一些,也就是说对象生命周期结束后并不立即释放内存。另一种说法是:在对象的内存分配后,可以不立即进行初始化,在对象销毁以后,可以不立即释放内存。在对象的生命周期已经结束了,但是内存还没释放的这段时间里,可以通过 void* 指针操作这块内存,,但是不允许对对象的 no-static成员进行访问,取地址以及其它操作。

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.

  • 堆区:
    正如你所知道的那样,堆区就是动态分配内存区域中的“另一种”,由 malloc/free 以及它们的变形形式进行申请和释放。注意有些编译器会使用 malloc/free来实现全局默认的 new/delete,但是自由存储区和堆区并不一样。因此 用malloc申请的内存,用delete进行释放是不安全的(事实上是及其危险的),反之亦然。但是在构造对象时也可以使用堆区的内存,哈哈,没错就是用 explicit destruction (显式析构函数) 和 placement-new(定位new)。如果这样做,自由存储区中关于对象生命周期中的规则在这儿也同样适用。

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.

  • 全局静态区:
    全局或静态变量和对象在程序启动时就会分配内存空间,但是可能直到程序开始执行后才会被初始化。例如,一个在函数中的中的static变量(也就是 local static变量)直到在程序第一次执行到它的定义的时候才会初始化它。Effective C++中是这样描述的:C++保证,函数内的local static对象会在“该函数被调用期间”“首次遇上该对象的定义式”时被初始化。C++ 对定义于不同编译单元内的 no-loacl static 对象(及所有的全局变量)的初始化次序并无明确定义。特别要注意的是 全局对象(包括静态对象)之间的依赖关系是需要特殊管理的,例如可以像 Effective C++中所提供的方案,把它们分别写到一个函数里,此时就变成了 local static ,并返回指向它们的引用。所以,这也说明了全局静态区内的对象跟 free store里对对象生命周期的规定一样,未初始化的及超出对象生命周期的这块内存可以用 void*指针进行操作,但是对象的非静态成员变量和非静态成员函数不能被访问,取地址,引用及其它操作。

原文地址

C++中的内存区[译文]的更多相关文章

  1. c/c++程序中内存区划分

    转自:http://wenzongliang.iteye.com/blog/1866629 操作系统启动程序时会加载程序代码到内存(叫程序的代码区),然后创建进程PCB为进程分配内存资源(数据区,32 ...

  2. 【C/C++】C/C++中的内存四区

    1 代码区 存放 CPU 执行的机器指令.通常代码区是可共享的(即另外的执行程序可以调用它),使其可共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可.代码区通常是只读的,使其只读的原因是 ...

  3. Java中堆内存和栈内存详解2

    Java中堆内存和栈内存详解   Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,ja ...

  4. Java中堆内存和栈内存详解

    Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...

  5. JavaScript 中对内存的一些了解

    在使用JavaScript进行开发的过程中,了解JavaScript内存机制有助于开发人员能够清晰的认识到自己写的代码在执行的过程中发生过什么,也能够提高项目的代码质量.其实关于内存的文章也有很多,写 ...

  6. Java中OutOfMemoryError(内存溢出)的三种情况及解决办法

    转载自:http://blog.sina.com.cn/s/blog_701c951f0100n1sp.html 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题, ...

  7. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  8. windows进程中的内存结构[转载]

    在阅读本文之前,如果你连堆栈是什么多不知道的话,请先阅读文章后面的基础知识. 接触过编程的人都知道,高级语言都能通过变量名来访问内存中的数据.那么这些变量在内存中是如何存放的呢?程序又是如何使用这些变 ...

  9. iOS开发中的内存分配(堆和栈)

    进程的内存分区 所有进程(执行的程序)都必须占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内存是事先 ...

随机推荐

  1. Unity3D必备知识: 物理学公式

    一.质点的运动(1)——直线运动 1)匀变速直线运动 1.平均速度V=s/t(定义式) 2.有用推论Vt*Vt-Vo*Vo=2as 3.中间时刻速度Vt/2=V平=(Vt+Vo)/2 4.末速度Vt= ...

  2. 【微信小程序】支付过程详解

    一.介绍 今天跟大家分享微信小程序集成支付. 二.分析 1.小程序支付API 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html ...

  3. string与wstring互转

    string与wstring互转  C++ Code  123456789101112131415161718192021222324252627282930313233343536373839404 ...

  4. SPAF模板

    #include <iostream> #include <cstring> #include <queue> #include <cstdio> #d ...

  5. Java知识点梳理——多态

    1.定义:多态是同一个行为具有多个不同表现形式或形态的能力,即一个接口不同的实例执行不同的操作: 2.优点:消除类型之间的耦合关系.可替换性.可扩展性.接口性.灵活性.简化性: 3.多态存在的3个必要 ...

  6. Manacher模板,kmp,扩展kmp,最小表示法模板

    *N]; //储存临时串 *N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); ; ;i<len;i++) { s[cnt++]= ...

  7. vs2017 git 操作重置、还原、挑拣对比

    工具 :vs2017 git 操作 背景:本地与远程分支同步 操作:还原.挑拣.重置--hard .重置--mixed 分支:本地1.本地2.origin\本地1   基本操作 1:分支:本地2-ad ...

  8. Asp.Net WebApi核心对象解析

    在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...

  9. 【BZOJ4597】[Shoi2016]随机序列 线段树

    [BZOJ4597][Shoi2016]随机序列 Description 你的面前有N个数排成一行.分别为A1, A2, … , An.你打算在每相邻的两个 Ai和 Ai+1 间都插入一个加号或者减号 ...

  10. Cocos2d-x学习笔记:CCSprite的使用总结

    一.精灵创建及初始化 备注:默认锚点ccp(0.5,0.5),默认位置 ccp(0,0),contentSize为精灵图片尺寸 1.从图片文件创建 2.从帧缓存创建: 3.初始化及自定义大小 4. 从 ...