Interview Questions: Allocate an object in the specified memory location

Source: Internet
Author: User
I encountered an interview question today to request that an object be allocated at the specified void * pmem in the memory so that the object can be stored at pmem. I am not doing this anymore. The record is as follows.
By default, the new operator placement of objects is used to create an object in free memory. If you want to allocate objects in the specified place, you should use the method described here. See the following example:

Class X {

Public: X (INT );//...}; when you need to place an object in a specified place, you only need to provide an additional parameter (both the specified memory address) for the allocation function ), then you can provide such an additional parameter when using new. See the following example:

Void * operator new (size_t, void * P) {return P;} // display placement Operator

Void * Buf = reinterpret_cast <void *> (0xf00f); // an important address

X * P2 = new (BUF) x; // create an X object at the Buf address,

// Call the operator new (sizeof (x), Buf) Function)

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.