Principle Analysis of New, nothrow new, and placement new

Source: Internet
Author: User
There are three ways to use new: Plain new, nothrow new, and placement new.

From: http://blog.sina.com.cn/s/blog_446b43c10100d7ci.html
1. Plain new/Delete. Common New
Definition:
Void * operator new (STD: size_t) Throw (STD: bad_alloc );

Void operator Delete (void *) Throw ();

Note: After standard C ++ plain new fails, a standard exception STD: bad_alloc is thrown instead of null. Therefore, it is futile to check whether the allocation is successful if the returned value is null.

TestProgram:

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Char * getmemory (unsigned Long SIZE)
{
Char * P = new char [size]; // allocation failed, not null returned
Return P;
}

Int main ()
{
Try
{
Char * P = getmemory (10e11); // an error is thrown when the allocation fails. STD: bad_alloc
//...........
If (! P) // in vain
Cout <"failure" <Endl;
Delete [] P;

}
Catch (const STD: bad_alloc & Ex)
{
Cout <ex. What () <Endl;
}

Return 0;
}

2. nothrow new/delete does not throw the format of the exception operator new. If new fails, null is returned.

Definition:

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 new iconic dummy Element

Test procedure:

# Include "stdafx. H"
# Include <iostream>
# Include <New>
Using namespace STD;

Char * getmemory (unsigned Long SIZE)
{
Char * P = new (nothrow) Char [size]; // allocation failed, returns 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 is mainly used to construct different types of objects or their arrays by repeatedly using a large dynamically allocated memory. For example, you can apply for a character array that is large enough, and then construct different types of objects or arrays on it as needed. Placement new does not need to worry about memory allocation failure because it does not allocate memory at all. It only calls the object constructor.

Test procedure:

# 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: Do not worry about failure
// Delete Q; // error! You cannot call Delete Q here;
Q-> ADT ::~ ADT (); // display the calls to the destructor
Delete [] P;
Return 0;
}

Note: objects or arrays constructed using placement new must be explicitly called to destroy their destructor (The Destructor does not release the object's memory). Do not use Delete. this is because the size of the objects or arrays constructed by placement new is not necessarily equal to the size of the originally allocated memory. Using Delete may cause memory leakage or a runtime error when the memory is released.

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.