Examples of C ++ operator new usage and operator instances

Source: Internet
Author: User

Examples of C ++ operator new usage and operator instances

For memory allocation in C ++, you can use C-style malloc and free, or use new and delete. I used to turn around an article to describe the differences between them in detail. Today, we will briefly describe the three usage methods of the new operator. In the past two days, a thread memory pool is being implemented using placement new to improve program performance. We will sort out the code in another day, put it on GitHub.

There are three new forms:

C ++ 98

(1) void* operator new (std::size_t size) throw (std::bad_alloc);(2) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();(3) void* operator new (std::size_t size, void* ptr) throw();

C ++ 11

(1) void* operator new (std::size_t size);(2) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;(3) void* operator new (std::size_t size, void* ptr) noexcept;

The first method is to allocate the storage space of size bytes. If successful, a non-null pointer is returned, pointing to the first byte of the allocated space. If it fails, a bad_alloc exception is thrown. The second form is the same as the first one. The difference is that if an exception is not thrown, a null pointer is returned. The third form only returns the ptr pointer and does not allocate memory space. The ptr here should point to the previously allocated space. Here, the new calls the object constructor and constructs the object or object array in the memory space pointed to by the ptr. If the ptr points to the memory, it can be reused as long as it is not released. Therefore, this method is generally used in the object pool or memory pool implementation.

Sample Code:

// Operator new example # include
 
  
// Std: cout # include
  
   
//: Operator newstruct MyClass {int data [100]; MyClass () {std :: cout <"constructed [" <this <"] \ n" ;}}; int main () {std: cout <"1 :"; myClass * p1 = new MyClass; // call operator new (sizeof (MyClass) to allocate memory, and then construct a new object std :: cout <"2:"; MyClass * p2 = new (std: nothrow) MyClass; // call operator new (sizeof (MyClass), std: nothrow) to allocate memory, then construct the new object std: cout <"3:"; new (p2) MyClass; in the newly allocated memory space; // do not allocate memory -- call operator new (sizeof (MyClass), p2) to construct a new object in the memory space pointed to by p2 // in the following way, the operator new operator is called, memory is allocated, but the constructor std: cout of MyClass is not called <"4:"; MyClass * p3 = (MyClass *):: operator new (sizeof (MyClass); delete p1; delete p2; delete p3; return 0 ;}
  
 

Execution result:

1: constructed [0x7faf5ec02920]2: constructed [0x7faf5ec02ab0]3: constructed [0x7faf5ec02ab0]4: 

It can be seen that the object structures of 2nd and 3rd Times are in the same memory space, that is, the same memory is reused.

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.

Sample Code:

// Operator new example # include
 
  
// Std: cout # include
  
   
//: Operator newstruct MyClass {int data [100]; MyClass () {std :: cout <"constructed [" <this <"] \ n" ;}}; int main () {std: cout <"sizeof (MyClass ): "<sizeof (MyClass) <std: endl; // allocate 2 * sizeof (MyClass) bytes of memory space char * pArray = new (std: nothrow) char [sizeof (MyClass) * 2]; char * p = pArray; // The first object constructs MyClass * p1 = new (p) MyClass from the memory position; // move the pointer position and construct the second object p + = sizeof (MyClass) in the new position; MyClass * p2 = New (p) MyClass; // call the Destructor p1 explicitly-> MyClass ::~ MyClass (); p2-> MyClass ::~ MyClass (); // delete [] pArray; return 0 ;}
  
 

Execution result:

sizeof(MyClass): 400constructed [0x7fc0dd402920]constructed [0x7fc0dd402ab0]

A MyClass occupies 400 bytes. The address of the first object is exactly 400 bytes different from that of the second object (0x7fc0dd402ab0-0x7fc0dd402920 = 0x190 ).

In the second example, the execution result is the same as the memory address of the first two objects in the first example, which may be different on different machines.

Related Article

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.