Different meanings: New and delete

Source: Internet
Author: User

C ++ has three types of memory management-related interfaces: new operator, new operator, and placement new.

The following describes the differences and connections between the three interfaces.

The new operator is a built-in operator of the language. Like sizeof, we cannot change its meaning and functions.. We use the new operation to create a dynamic object. It mainly completes two parts:

1. allocate enough memory to accommodate the required types of objects.

2. Call the constructor to initialize objects in the memory.

When allocating memory with the new operator, you need to call a memory allocation function to complete memory allocation. This function is the new operation (operator new ).

The operator new function is declared as follows:

1 Void*Operator New(Size_t size );

We customize new and delete mainly refers to rewriting or reloading this function to change its behavior.

Sometimes operator new does not complete our work. For example, we need to record the allocation information and create objects on the existing shared memory. At this time, we need placement new.

If operator new accepts other parameters except the size_t, the operator new can be called Placement new..

Among the many placement new versions, one of the most useful is "accepting a pointer pointing to the object to be constructed". The declaration is as follows:

1 Void*Operator New(STD: size_t,Void* Pmemory)Throw();

In general,To create an object on the stack, use the new operator. it allocates both memory and calls constructors for objects. if you only want to allocate memory, you should call the operator new function, which does not call the constructor. if you want to customize your memory allocation process when the heap object is created, you should write your own operator new function and then use the new operator. The new operator calls the customized operator new. if you want to create an object in a memory with obtained pointers, you should use placement new.

The differences and relationships between memory-released interfaces, delete operations, delete operators, and placement Delete are similar to those of the allocation interface.

When using the memory management interface and new and delete customization, we should pay attention to two issues:

1. interfaces for memory allocation and memory release are supported. When we write our placement new, we must write the placement Delete with the same additional parameters.

2. When you customize your memory management interfaces, you must avoid masking the standard format.. Because standard versions may be called to complete customized exclusive versions.

1   Class  Standardnewdeleteforms {  2       Static   Void * Operator   New (STD: size_t size) Throw  (STD: bad_alloc)  3   {  4           Return :: Operator   New (Size );  5   }  6       Static   Void   Operator Delete ( Void * Pmemory) Throw  ()  7   {  8 :: Operator  Delete (pmemory );  9  }  10       Static   Void * Operator   New (STD: size_t size, Void * PTR) Throw  ()  11   {  12           Return :: Operator   New  (Size, PTR ); 13   }  14       Static   Void   Operator Delete ( Void * Pmemory, Void * PTR) Throw  ()  15   {  16 :: Operator  Delete (pmemory, PTR );  17  }  18       Static   Void * Operator   New (STD: size_t, Const STD: nothrow_t & NT) Throw  ()  19   {  20           Return :: Operator   New  (Size, NT ); 21   }  22       Static   Void * Operator Delete ( Void * Pmemory, Const STD: nothrow_t &) Throw  ()  23   {  24 :: Operator  Delete (pmemory ); 25   }  26   };  27   28   Class Widget: Public  Standardnewdeleteforms {  29   Public  :  30       Using Standardnewdeleteforms :: Operator   New ;  31       Using Standardnewdeleteforms :: Operator  Delete;  32       33       Static   Void * Operator   New (STD: size_t size, STD: ostream & logstream) Throw  (STD: bad_alloc );  34       Static  Void   Operator Delete ( Void * Pmemory, STD: ostream & logstream) Throw  ();  35   ...  36 };


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.