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