snail关于C++指针与内存管理的一些备忘
Author:snail
有点乱,而且还在整理中~~最近一直在关注程序的内存管理~所以把一些看过的资料做个
索引,做个备忘 ---------------snail 07年3月2日
(希望璐璐看了会有点帮助,这下面的资料有任何问题都可以问我)
http://blog.youkuaiyun.com/snailjava/archive/2007/03/02/1519069.aspx
一篇很不错的文章,简单易懂 。
2 <<C++程序设计语言(特别版)>>中关于内存的分配的一些资料
6.2.6 自由存储
10.4.5 自由存储
********************************************************************************************
特别是,如果知道存在有废料收集器,那些只做存储管理的析构函数就都可以去掉了:这种简化所付出的代价是可移植性,对某些程序可能增加运行时间,并有可能丧失对于其运行时间性质的可预见性
---B.S对带垃圾收器的C++的评价,真是一针见血啊!
********************************************************************************************
10.4.11 对象的放置
15.6 自由存储
********************************************************************************************
[虚构造函数]
“要构造一个对象,构造函数必须掌握所创建对象的确切类型。因此,构造函数不能是虚的。构造函数并不是一个普通的函数。特别是它需要与存储管理例程打交道,而且是以常规成员函数所没有的方式。因此,你不能有一个到构造函数的指针。这两个限制(上面两行红色的字)都可以通过迂回方式绕过去,方法就是定义一个函数,由它调用构造函数并返回构造起来的对象。这是很幸运的,因为创建一个对象而又不必知道其确切类型,这常常也很有意义。(snail:而且应该非常有趣J)
19.4.5 动态存储
B.3.4 分配错误
********************************************************************************************
X *p1 = new X; //没有存储时抛出bad_alloc
X* p2 = new(nothrow) X; //没有存储时返回0
********************************************************************************************
//参考B.S<<C++程序设计特别版>>19.4.5 #include <iostream> #include <cstdio>
using namespace std;
int main(){ int* p =new int[200000000];
if(int *q = new(nothrow) int[200000000]){ cout << "success!"; //显示success至少需要多少内存?考考你 }else{ cout << "lost!"; }
system("pause"); } //我的机器跑出来是success!内存还蛮大的哦 |
3 <<effective C++>>第二章 内存管理
(snail:很喜欢书里面对内存管理的描述)
“c++中涉及到的内存的管理问题可以归结为两方面:正确地得到它和有效地使用它。好的程序员会理解这两个问题为什么要以这样的顺序列出。因为执行得再快、体积再小的程序如果它不按你所想象地那样去执行,那也一点用处都没有。“正确地得到”的意思是正确地调用内存分配和释放程序;而“有效地使用”是指写特定版本的内存分配和释放程序。这里,“正确地得到”显得更重要一些。” --<<effective C++>>
条款5:对应的new和delete要采用相同的形式
条款6:析构函数里对指针成员调用delete
条款7:预先准备好内存不够的情况
条款8: 写operator new和operator delete时要遵循常规
条款9: 避免隐藏标准形式的new
条款10: 如果写了operator new就要同时写operator delete 。
(重要!:条款10解释了为什么有时要重写operator new)
4 理解了上面1,2,3以后
如何在linux下检测内存泄漏
http://www.ibm.com/developerworks/cn/linux/l-mleak/index.html
一个跨平台的 C++ 内存泄漏检测器
http://www.ibm.com/developerworks/cn/linux/l-mleak2/index.html
5 一个内存管理的专题网站
snail: 竟然被我找到一个内存管理的专题网站!!(里面的内容非常详细啊)
http://www.memorymanagement.org/
The Memory Management Reference Beginner's Guide Overview
http://www.memorymanagement.org/articles/begin.html
FAQ
http://www.memorymanagement.org/faq.html
附录:一些名词解释:
Stack allocation means run-time allocation and deallocation of storage in last-in/first-out order. Typically, stack allocation is perfored on top of the main stack, but one can have a separate data stack for this purpose as well, as in Forth, or even multiple ones, as in the PostScript® language. Allocation and deallocation are typically fast, since they can be done simply by adding or subtracting the size of the block from the stack pointer. Using only stack allocation, without heap allocation, is somewhat restrictive, as only objects whose size is known at compile-time can be returned from a procedure. Some programming languages (such as some versions of Lisp and C) provide program-controlled stack allocation and deallocation of dynamic extent objects for efficiency, despite its being unsafe. Similar terms: automatic storage duration.
Static allocation means allocation of storage before the program starts and retention until the end. The locations of objects are basically decided at compile-time, although they might be relocated at load-time. This implies the sizes of the objects must be known then. Using only static allocation is restrictive, as sizes of data structures can't be dynamically varied, and procedures cannot be recursive. However, it is also fast and eliminates the possibility of running out of memory. For this reason, this scheme is sometimes used in real-time systems. Historical note: The first high-level language, Fortran, only had static allocation to begin with. Later languages usually offer heap and/or stack allocation, but static allocation is often available as an option. Similar terms: static storage duration.
heap (also known as free store, freestore) The heap or free store is the memory(2) area managed by dynamic allocation. This use of heap is unconnected with the data structure used by the heapsort algorithm
heap allocation (also known as dynamic allocation) Heap allocation or dynamic allocation means run-time allocation and deallocation of storage in arbitrary order. Dynamic allocation is usually for objects whose size, quantity, or lifetime could not be determined at compile-time. It is necessary to implement modern data structures, such as recursive trees and full closures. Objects on the heap can be managed manually, as in C, or automatically, as in Lisp and JavaTM. Opposites: stack allocation; static allocation.
|