C中运算符New的三种使用方式.docx
- 文档编号:23966539
- 上传时间:2023-05-23
- 格式:DOCX
- 页数:8
- 大小:15.81KB
C中运算符New的三种使用方式.docx
《C中运算符New的三种使用方式.docx》由会员分享,可在线阅读,更多相关《C中运算符New的三种使用方式.docx(8页珍藏版)》请在冰豆网上搜索。
C中运算符New的三种使用方式
C++中运算符New的三种使用方式
这是在林锐的《高质量程序设计指南》中看到的,特此记录下。
1.plainnew普通new
Cpp代码
<spanstyle="font-size:
medium;">void*operatornew(std:
:
size_t)throw(std:
:
bad_alloc);
voidoperatordelete(void*)throw();
</span>
void*operatornew(std:
:
size_t)throw(std:
:
bad_alloc);
voidoperatordelete(void*)throw();
该运算符在分配失败时将抛出异常,而非返回NULL。
使用时要包含<new>头文件。
正常使用new,但要配以异常处理。
如:
Cpp代码
<spanstyle="font-size:
medium;">char*getMemory(unsignedlongsize)
{char*p=newchar[size];
returnp;}
voidmain(void)
{try{
char*p=getMemory(1000000);//可能发生异常
//...
delete[]p;
}
catch(conststd:
:
bad_alloc&ex)
{cout<<ex.what();}
}</span>
char*getMemory(unsignedlongsize)
{char*p=newchar[size];
returnp;}
voidmain(void)
{try{
char*p=getMemory(1000000);//可能发生异常
//...
delete[]p;
}
catch(conststd:
:
bad_alloc&ex)
{cout<<ex.what();}
}
2.nothrownew不抛掷异常new
Cpp代码
<spanstyle="font-size:
medium;">void*operatornew(std:
:
size_t,conststd:
:
nothrow_t&)throw();
voidoperatordelete(void*)throw();</span>
void*operatornew(std:
:
size_t,conststd:
:
nothrow_t&)throw();
voidoperatordelete(void*)throw();
该运算符在分配失败时不抛出异常,而是返回NULL。
使用时要包含<new>头文件。
该函数的第2形参是structnothrow_t{};它是个全局常对象constnothrow_tnothrow;用来作为new运算符的标志,以区别前一个new.
Cpp代码
<spanstyle="font-size:
medium;">voidfunc(unsingedlonglength)
{
unsingedchar*p=new(nothrow)unsingedchar[length];
//在使用这种new时要加(nothrow),明示不使用异常处理。
if(p==NULL)//因不抛异常,故定要检查
cout<<“alloctefailed!
”;
//...
delete[]p;
}</span>
voidfunc(unsingedlonglength)
{
unsingedchar*p=new(nothrow)unsingedchar[length];
//在使用这种new时要加(nothrow),明示不使用异常处理。
if(p==NULL)//因不抛异常,故定要检查
cout<<“alloctefailed!
”;
//...
delete[]p;
}
3.placementnew放置new
Cpp代码
<spanstyle="font-size:
medium;">void*operatornew(std:
:
size_t,void*);
voidoperatordelete(void*,void*);</span>
void*operatornew(std:
:
size_t,void*);
voidoperatordelete(void*,void*);
该运算符是在已分配的内存上重新构造对象,因为不分配内存,所以不必担心分配失败。
唯一的工作是调用构造函数。
要包含<new>头文件。
Cpp代码
<spanstyle="font-size:
medium;">#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[4];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
//...
long*q=new(p)long(1000);
delete[]p;//只释放p,不要用q释放。
}</span>
#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[4];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
//...
long*q=new(p)long(1000);
delete[]p;//只释放p,不要用q释放。
}
p和q仅仅是首址相同,所构建的对象可以类型不同。
所“放置”的空间应小于原空间,以防不测。
当”放置new”超过了申请的范围,Debug版下会挂机,但Release版竟然能运行而不出错!
该运算符的作用是:
只要第一次分配成功,不再担心分配失败。
Cpp代码
<spanstyle="font-size:
medium;">#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[100];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
long*q1=new(p)long(100);
//使用q1...
int*q2=new(p)int[100/sizeof(int)];
//使用q2...
ADT*q3=new(p)ADT[100/sizeof(ADT)];
//使用q3然后释放对象...
delete[]p;//只释放空间,不再析构对象。
}</span>
#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[100];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
long*q1=new(p)long(100);
//使用q1...
int*q2=new(p)int[100/sizeof(int)];
//使用q2...
ADT*q3=new(p)ADT[100/sizeof(ADT)];
//使用q3然后释放对象...
delete[]p;//只释放空间,不再析构对象。
}
注意:
使用该运算符构造的对象或数组,一定要显式调用析构函数,不可用delete代替析构,因为placementnew的对象的大小不再与原空间相同。
Cpp代码
<spanstyle="font-size:
medium;">#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[sizeof(ADT)+2];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
//...
ADT*q=new(p)ADT;
//...
//deleteq;//错误
q->ADT:
:
~ADT();//显式调用析构函数,仅释放对象
delete[]p;//最后,再用原指针来释放内存.
}</span>
#include<new>
#include<iostream>
voidmain()
{usingnamespacestd;
char*p=new(nothrow)char[sizeof(ADT)+2];
if(p==NULL)
{cout<<“alloctefailed”<<endl;exit(-1);}
//...
ADT*q=new(p)ADT;
//...
//deleteq;//错误
q->ADT:
:
~ADT();//显式调用析构函数,仅释放对象
delete[]p;//最后,再用原指针来释放内存.
}
placementnew的主要用途就是可以反复使用一块已申请成功的内存空间。
这样可以避免申请失败的徒劳,又可以避免使用后的释放。
特别要注意的是对于placementnew绝不可以调用的delete,因为该new只是使用别人替它申请的地方(只是个租房户,不是房主。
无权将房子卖掉)。
释放内存是nothrownew的事,即要使用原来的指针释放内存.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中运算符New的三种使用方式 运算 New 使用 方式
![提示](https://static.bdocx.com/images/bang_tan.gif)