第十六章 内存管理 ===高质量C/C++编程指南

来源:互联网 发布:js table的高度 编辑:程序博客网 时间:2024/06/09 14:24

New的3种使用方式:

Plain new // nothrow new //  placement new


Plain new :

                void* operator(std::size_t)throw(std::bad_alloc);

                void operator delete(void*)throw();

标准C++修订new的语义,plain new在失败后抛出异常std::bad_alloc而不是返回NULL。所以通过其返回值来判断其返回值是徒劳的。 

char* getmemory(unsigned long len){    char* p=new char[len];    return p;}int main(){    try{       char* p=getmemory(10000000000000000000);       delete [] p;    }    catch(const std::bad_alloc &ex){       cout<<ex.what()<<endl;    }    return 0;}


Nothrow new:

                    顾名思义,不抛出任何异常的运算符,Nothrow new失败返回NULL,所以使用时就不必设置异常处理器,用if检查是否为NULL即可。

                   void* operator new(std::size)_t,const std::nothrow_t&)throw();

                   void operator delete(void*)throw();

nothrow带有一个参数,类型为nothrow_t <NEW> 的一个全局const对象,作为标志哑元。

char* getmemory(unsigned long len){    char* p=new(nothrow) char[len];    return p;}int main(){       char* p=getmemory(10000000000000000000);        if(!p) cout<<"alloc failed!"<<endl;       delete [] p;        return 0;}

placement new/delete

                       placement new在一块已经分配好的内存上面重新构造对象或对象数组,不用担心分配失败,因为根本没有分配。它所做的就是调用对象的构造函数。在<NEW>中定义原型如下:

                     void* _operator new(size_t,void*)

                     void* _operator delete(void*,void*)

 用法: typename *q=new(p) typename; 将p转换为目标类型的指针q;

因此p和q相等。

#include <iostream>#include<NEW>using namespace std;char* getmemory(unsigned long len){    char* p=new(nothrow) char[len];    return p;}class A{           int a;public:          A(int a=0):a(a){cout<<"hhaha"<<endl;}          int getnum()const{return a;}          ~A(){}};int main(){        char* p=getmemory(100);        if(!p) cout<<"alloc failed!"<<endl;         double* q=new(p)double(3);         cout<<*q<<endl;        A* r=new(p)A(10);        r->A::~A();        delete p;        return 0;}
用途:反复使用一块较大的动态分配成功的内存来构造不同类型的对象或者他们的数组,比如,可以先申请一个足够大的字符数组,然后当需要时在它上面构造不同类型对象或者其数组。

注意:使用placement new构造起来的对象或者数组,要显式的调用他们的析构函数来销毁(析构函数并不释放对象的内存),因为对象或对象数组的大小并不等于原来分配内存的大小。因此使用delete会内存泄露。




0 0
原创粉丝点击