Placement new operator Learning in C + +

Source: Internet
Author: User

Reference: http://blog.csdn.net/zhangxinrun/article/details/5940019

New operator:

The example is as follows: int * Pint=new int (23); It allocates memory space on the heap, initializes the memory space, and finally returns the pointer.

For this operation to be overloaded, remember that it can only be overloaded in the class, get an overloaded version of placement new (old version is operator new), prototype as follows void *operator new (size_t, void *p) throw () {RE Turn p; }, Applied: MyClass * PCLASS=NEW (P) Int (23), formal and original is different, its function is simply to implement the existing memory of the pointer p on the construction of an int object.

Comparison:

Obviously there is less memory space allocation process, saving the process of memory search; As for memory applications, there can be other functions to do, such as using the previous new, but only in its space to re-specify, and do not need to destroy, redistribution again.

Attention:

Here the memory can be in the heap, of course, it can be in the stack, here is new, for new[], delete, delete[] are similar to the processing.

Placement new operator Learning in C + +

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.