There are three ways to use NEW: plain New, nothrow new, placement new.

Source: Internet
Author: User
1.plain New/delete. Ordinary New
The definition is as follows:
void *operator New (std::size_t) throw (Std::bad_alloc);

void operator Delete (void*) throw ();

Note: Standard C + + plain throws a standard exception std::bad_alloc instead of returning null when new fails, so it is futile to check that the return value is null to determine whether the assignment succeeded.


2.nothrow New/delete does not throw an exception in the form of operator new, which returns NULL when new fails.

The definition is as follows:

void *operator New (Std::size_t,const std::nothrow_t&) throw ();

void operator Delete (void*) throw ();

struct nothrow_t{}; Const nothrow_t Nothrow;//nothrow As the symbolic dummy of new

Test program:

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace Std;

Char *getmemory (unsigned long size)
{
Char *p=new (nothrow) char[size];//allocation failed, return 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 &AMP;EX)
{
Cout<<ex.what () <<endl;
}

return 0;
}


The main purpose of 3.placement New/delete is to construct different types of objects or their arrays repeatedly using a large dynamically allocated memory. For example, you can request an array of characters that is large enough, and then construct different types of objects or arrays on top of it when needed. Placement new does not have to worry about memory allocation failure because it does not allocate memory at all, it simply invokes the object's constructor.

Note: objects or arrays constructed with placement new will be explicitly called to destroy (destructors do not release the object's memory), and do not use Delete. This is because placement The new constructed object or array size does not necessarily equal the size of the original allocated memory, and using delete causes a memory leak or a run-time error when memory is released later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.