当前位置: 代码迷 >> 综合 >> c++中new的三种用法详解 清晰明了
  详细解决方案

c++中new的三种用法详解 清晰明了

热度:79   发布时间:2024-01-29 15:47:26.0

一、概述

new有三种使用方式:plain new,nothrow new和placement new。

(1)plain new就是我们平常使用的普通的new运算符。

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

提示:plain new在分配失败的情况下,抛出异常std::bad_alloc而不是返回NULL

程序用例:

#include "stdafx.h"
#include <iostream>
using namespace std;char *CreateMemory(unsigned long size)
{char *p=new char[size];//分配失败,不是返回NULLreturn p;
}int main()
{try{char *p=CreateMemory(10e11);// 分配失败抛出异常std::bad_alloc//...........if(!p)//不能通过p 是否等于NULL来判断内存分配的失败与否cout<<"failure"<<endl;delete [] p;}catch(const std::bad_alloc &ex){cout<<ex.what()<<endl;}return 0;
}

(2)nothrow new是不抛出异常的运算符new的形式。nothrow 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 *CreateMemory(unsigned long size)
{char *p=new(nothrow) char[size];//分配失败,是返回NULLif(NULL==p)cout<<"alloc failure!"<<endl;return p;
}int main()
{try{char *p=CreateMemory(10e11);//...........if(p==NULL)cout<<"failure"<<endl;delete [] p;}catch(const std::bad_alloc &ex){cout<<ex.what()<<endl;}return 0;
}

(3)placement new意即“放置”,这种new允许在一块已经分配成功的内存上重新构造对象或对象数组。例如可以先申请一个足够大的字符数组,然后当需要时在它上面构造不同类型的对象或数组。placement new不用担心内存分配失败,因为它根本不分配内存,它做的唯一一件事情就是调用对象的构造函数。定义如下:

void* operator new(size_t,void*);void operator delete(void*,void*);

注意:palcement new的主要用途就是反复使用一块较大的动态分配的内存来构造不同类型的对象或者他们的数组。           placement new构造起来的对象或其数组,要显示的调用他们的析构函数来销毁,千万不要使用delete。

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace std;class Base
{
public:Base(){}~Base(){}
private:char m_data;};int main()
{char *p=new(nothrow) char[sizeof(Base)+2];if(p==NULL)cout<<"failure"<<endl;Base* q=new(p) Base;  //placement new:不必担心失败// delete q;//错误!不能在此处调用delete q;q->Base::~Base();//显示调用析构函数delete []p;return 0;
}


注:使用placement new构造起来的对象或数组,要显式调用它们的析构函数来销毁(析构函数并不释放对象的内存),千万不要使用delete.这是因为placement new构造起来的对象或数组大小并不一定等于原来分配的内存大小,使用delete会造成内存泄漏或者之后释放内存时出现运行时错误。

  相关解决方案