Three usage methods of the new operator in C ++

Source: Internet
Author: User

From: http://badboywang.iteye.com/blog/446350
1. Plain new common new

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

This operator throws an exception when the allocation fails, instead of returning null. The <New> header file must be included during use. New is used normally, but it must be configured with exception handling. For example:

Char * getmemory (unsigned Long SIZE) {char * P = new char [size]; return P;} void main (void) {try {char * P = getmemory (1000000 ); // an exception may occur //... delete [] P;} catch (const STD: bad_alloc & Ex) {cout <ex. what ();}}

2. nothrow New: No throwing exception new

void*operator new(std::size_t,const std::nothrow_t & )throw();   void operator delete( void *) throw();

This operator does not throw an exception when the allocation fails, but returns NULL. The <New> header file must be included during use.
The 2nd parameter of this function is struct nothrow_t {}; it is a global common object const nothrow_t nothrow; used as a sign of the new operator to distinguish the previous new.
3. Place New to place new

>void*operator new(std::size_t ,void *);   void operator delete( void * ,void *); 

This operator re-constructs an object on the allocated memory. Because no memory is allocated, you do not have to worry about the allocation failure. The only task is to call the constructor. To include the <New> header file.

# Include <New> # include <iostream> void main () {using namespace STD; char * P = new (nothrow) Char [4]; If (P = NULL) {cout <"allocte failed" <Endl; exit (-1 );}//... long * q = new (p) Long (1000); Delete [] P; // release only P, do not use Q to release. }

P and q are only the same as the first address, and the constructed object can be of different types. The space to be placed should be smaller than the original space to prevent unexpected events. When "place new" exceeds the application range, the debug version will be suspended, but the release version can run without errors!
The function of this operator is: as long as the first allocation is successful, there is no longer a worry about allocation failure.

# Include <New> # include <iostream> void main () {using namespace STD; char * P = new (nothrow) Char [100]; If (P = NULL) {cout <"allocte failed" <Endl; exit (-1);} Long * Q1 = new (p) Long (100); // use Q1... int * q2 = new (p) int [100/sizeof (INT)]; // use Q2... ADT * Q3 = new (p) ADT [100/sizeof (ADT)]; // use Q3 and then release the object... delete [] P; // only releases space and no longer analyzes the object. }

Note: If you use this operator to construct an object or array, you must call the Destructor explicitly. You cannot use Delete to replace the Destructor because the size of the placement new object is no longer the same as that of the original space.

# Include <New> # include <iostream> void main () {using namespace STD; char * P = new (nothrow) Char [sizeof (ADT) + 2]; if (P = NULL) {cout <"allocte failed" <Endl; exit (-1 );}//... ADT * q = new (p) ADT ;//... // Delete Q; // error Q-> ADT ::~ ADT (); // explicitly call the destructor, only releasing the object Delete [] P; // Finally, use the original pointer to release the memory .}

Placement new is mainly used to repeatedly use a memory space that has been successfully applied. In this way, you can avoid application failure in vain and release after use.

Note that placement new cannot be called with the delete function, because the new function is only used by someone else to apply for it (only a tenant, not the homeowner. You are not authorized to sell the house ). Releasing memory is a task of nothrow new, that is, to use the original pointer to release the memory

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.