C++ Memory Management C++ 内存管理

来源:互联网 发布:天天飞车礼包 软件 编辑:程序博客网 时间:2024/06/05 06:21

Problem

C++ has several distinct memory areas where objects and non-object values may be stored, and each area has different characteristics.

Name as many of the distinct memory areas as you can. For each, summarize its performance characteristics and describe the lifetime of objects stored there.

Example: The stack stores automatic variables, including both builtins and objects of class type.

Solution

The following summarizes a C++ program's major distinct memory areas. Note that some of the names (e.g., "heap") do not appear as such in the draft [standard].

 Memory Area     Characteristics and Object Lifetimes --------------  ------------------------------------------------ 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.

Note about Heap vs. Free Store: We distinguish between "heap" and "free store" because the draft deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via operator delete, 18.4.1.1 states:

It is unspecified under what conditions part or all of such reclaimed storage is allocated by a subsequent call to operator new or any of calloc, malloc, or realloc, declared in <cstdlib>.

Similarly, it is unspecified whether new/delete is implemented in terms of malloc/free or vice versa in a given implementation. Effectively, the two areas behave differently and are accessed differently -- so be sure to use them differently!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果开机密码忘了怎么办 我的ipad很卡怎么办 6s升级后卡顿严重怎么办 软件升级后手机卡顿怎么办 ps通道抠图模糊怎么办 电脑锁屏死机了怎么办 ipad电源键坏了怎么办 华为平板死机黑屏了怎么办 苹果4s锁屏键坏了怎么办 平板电脑密码锁忘记密码怎么办 苹果平板电脑密码锁忘记密码怎么办 平板电脑密码锁密码锁死怎么办 小米平板黑屏按键亮怎么办 小米平板黑屏没反应怎么办 小米平板一刷黑屏了怎么办 小米4卡死了怎么办啊 苹果平板电脑开不了机怎么办 ipad开不了机怎么办都是黑屏 手机突然黑屏了死机状态怎么办 苹果平板死机不能重启怎么办 韩众平板死机了怎么办 苹果平板输入密码多次停用怎么办 ld密码被停用了怎么办 档案被自己丢了怎么办 手机在厂里丢了怎么办 在厂里借工具丢了怎么办 导出的考勤没有姓名怎么办 退休时档案丢了怎么办 职工与企业没有劳资怎么办 去大学报道的档案袋丢失怎么办 档案入学毕业年份写错怎么办 从事业单位辞职后人事档案怎么办 老师辞职不给批怎么办 公办教师去私立学校档案怎么办 辞职后档案不给怎么办 档案不小心拆了怎么办 退休职工档案年龄有涂改怎么办 养老金原始档案找不到怎么办退休 寄辞职信不接收怎么办 公司不给办离职怎么办 离职手续表填写错误怎么办