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)