What is an in-place constructor in C++?
e.g. Datatype *x = new(y) Datatype();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is called the placement new operator. It allows you to supply the memory the data will be allocated in without having the
newoperator allocate it. For example:The above will allocate memory for you.
The above will use the memory allocated by the call to
malloc.newwill not allocate any more. You are not, however, limited to classes. You can use a placement new operator for any type you would allocate with a call tonew.A ‘gotcha’ for placement new is that you should not release the memory allocated by a call to the placement new operator using the
deletekeyword. You will destroy the object by calling the destructor directly.After the destructor is manually called, the memory can then be freed as expected.