转自:http://blog.sina.com.cn/s/blog_446b43c10100d7ci.html
1.plain new/delete.普通的new
定义如下:
void *operator new(std::size_t) throw(std::bad_alloc);
void operator delete(void*) throw();
注:标准C++ plain new失败后抛出标准异常std::bad_alloc而非返回NULL,因此检查返回值是否为NULL判断分配是否成功是徒劳的。
测试程序:
#include "stdafx.h"
#include <iostream>
using namespace std;
char *GetMemory(unsigned long size)
{
char *p=new char[size];//分配失败,不是返回NULL
return p;
}
int main()
{
try
{
char *p=GetMemory(10e11);// 分配失败抛出异常std::bad_alloc
//...........
if(!p)//徒劳
cout<<"failure"<<endl;
delete [] p;
}
catch(const std::bad_alloc &ex)
{
cout<<ex.what()<<endl;
}
return 0;
}
2.nothrow new/delete不抛出异常的运算符new的形式,new失败时返回NULL。
定义如下:
void *operator new(std::size_t,const std::nothrow_t&) throw();
void operator delete(void*) throw();
struct nothrow_t{}; const nothrow_t nothrow;//nothrow作为new的标志性哑元
测试程序:
#include "stdafx.h"
#include <iostream>
#include <new>
using namespace std;
char *GetMemory(unsigned long size)
{
char *p=new(nothrow) char[size];//分配失败,是返回NULL
if(NULL==p)
cout<<"alloc failure!"<<endl;
return p;
}
int main()
{
try
{
char *p=GetMemory(10e11);
//...........
if(p==NULL)
cout<<"failure"<<endl;
delete [] p;
}
catch(const std::bad_alloc &ex)
{
cout<<ex.what()<<endl;
}
return 0;
}
3.placement new/delete 主要用途是:反复使用一块较大的动态分配成功的内存来构造不同类型的对象或者它们的数组。例如可以先申请一个足够大的字符数组,然后当需要时在它上面构造不同类型的对象或数组。placement new不用担心内存分配失败,因为它根本不分配内存,它只是调用对象的构造函数。
测试程序:
#include "stdafx.h"
#include <iostream>
#include <new>
using namespace std;
class ADT
{
int i;
int j;
public:
ADT()
{
}
~ADT()
{
}
};
int main()
{
char *p=new(nothrow) char[sizeof(ADT)+2];
if(p==NULL)
cout<<"failure"<<endl;
ADT *q=new(p) ADT; //placement new:不必担心失败
// delete q;//错误!不能在此处调用delete q;
q->ADT::~ADT();//显示调用析构函数
delete []p;
return 0;
}
注:使用placement new构造起来的对象或数组,要显式调用它们的析构函数来销毁(析构函数并不释放对象的内存),千万不要使用delete.这是因为placement new构造起来的对象或数组大小并不一定等于原来分配的内存大小,使用delete会造成内存泄漏或者之后释放内存时出现运行时错误。
详细解决方案
new有三种使用方式:plain new、nothrow new、placement new.
热度:41 发布时间:2024-01-09 03:56:19.0
相关解决方案
- 奇怪的异常Resource interpreted as Script but transferred with MIME type text/plain
- 怎么用JAVA实现邮件发送?而且要实现LOGIN,PLAIN,CRAM-MD5三种认证方式
- ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN
- Antenna Placement POJ - 3020(二分图最大匹配最小边覆盖)
- postman请求报 Content type ‘text/plain;charset=UTF-8’ not supported
- spring框架controller方法接收 Content-Type=text/plain(text/json)格式类型的参数数据
- 【odoo10】Refused to execute script from '*' because its MIME type ('text/plain') is not executable
- E: Unable to locate package placement-api
- 引入CSS出现because its MIME type ('text/plain') is not executable错误
- operator new 与 placement new之间的微妙关系
- {“detail“:“不支持请求中的媒体类型 “text/plain”。“}
- placement new和placement delete(重载new和delete2)
- Resource interpreted as Stylesheet but transferred with MIME type text/plain:
- Resource interpreted as Stylesheet but transferred with MIME type text/plain
- OpenStack部署(四、placement)
- new有三种使用方式:plain new、nothrow new、placement new.
- ElasticSearch Shard Placement Control
- nothrow new
- POJ 3020 Antenna Placement (二分图匹配)
- 有关C++中的 placement new
- Try a post in plain text
- skynet demo plain text 分支(四)通讯协议的载入
- http请求心知天气400 The plain HTTP request was sent to HTTPS port解决方案
- kaggle:PUBG Finish Placement Prediction