[初]snail关于C++指针与内存管理的一些备忘

snail关于C++针与内存管理的一些

Author:snail

 

有点乱,而且还在整理中~~最近一直在关注程序的内存管理~所以把一些看过的资料做个

索引,做个备忘    ---------------snail   0732

(希望璐璐看了会有点帮助,这下面的资料有任何问题都可以问我)


 

1 C++存管理

http://blog.csdn.net/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对应newdelete要采用相同的形式

6:析员调delete

7先准存不的情

8: operator newoperator delete要遵循常

9: 避免准形式的new

10: 如果operator new就要同时写operator delete

 (重要!:10要重operator new)

 

 

 

4 理解了上面123以后

如何在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

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.
Opposites: heap allocation; static allocation.
See also: region inference; dynamic extent.

 

static allocation

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.
Opposites: stack allocation; heap allocation.
See also: region inference; static memory(2).

 

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.
See also: indefinite extent.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值