C ++ new, placement new, and nothrow new

Source: Internet
Author: User

 

There are three ways to use new: Plain new, nothrow new, and placement new.

(1) as the name suggests, plain new is a common new, which we usually use. In C ++, it is defined as follows:

Void * operator new (STD: size_t) Throw (STD: bad_alloc );

Void operator Delete (void *) Throw ();

Tip: When the allocation fails, plain new throws an exception STD: bad_alloc instead of returning null. Therefore, it is futile to determine whether the returned value is null.

(2) nothrow new is the form of the new operator that does not throw an exception. When nothrow new fails, null is returned. Definition:

Void * operator new (STD: size_t, const STD: nothrow_t &) Throw ();

Void operator Delete (void *) Throw ();

(3) Placement new indicates "placement". This new type allows you to reconstruct an object or an array of objects on a successfully allocated memory. Placement new does not need to worry about memory allocation failure because it does not allocate memory at all. The only thing it does is to call the object constructor. Definition:

Void * operator new (size_t, void *);

Void operator Delete (void *, void *);

Tip 1: The main purpose of palcement new is to repeatedly use a large dynamically allocated memory to construct different types of objects or their arrays.

Tip 2: Placement new constructs an object or its array. to display the objects, call their destructor to destroy them. Do not use Delete.

Char * P = new (nothrow) Char [100];

Long * Q1 = new (p) Long (1, 100 );

Int * q2 = new (p) int [100/sizeof (INT)];

Char * P = new (nothrow) Char [100];

Long * Q1 = new (p) Long (1, 100 );

Int * q2 = new (p) int [100/sizeof (INT)];

The three statements mean to apply for char [100] space with nothrow new, apply for a long type space in this space to store 100, and then an array of int type.

 

More reading links

Http://www.scs.stanford.edu /~ DM/home/papers/c00000000-new.html

Http://eli.thegreenplace.net/2011/02/17/the-many-faces-of-operator-new-in-c !!!!!!!

Http://en.wikipedia.org/wiki/Placement_syntax

Http://blog.csdn.net/huyiyang2010/article/details/5984987

Http://www.gotw.ca/publications/mill15.htm

Http://www.informit.com/guides/content.aspx? G = cplusplus & seqnum = 170

 

 

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.