31

Possible Duplicate:
C++'s “placement new”

What is an in-place constructor in C++?

e.g. Datatype *x = new(y) Datatype();

peterh
  • 11,875
  • 18
  • 85
  • 108
Akhil
  • 2,269
  • 6
  • 32
  • 39
  • 7
    This is also called _placement new_ and you might have better chances finding information for that name. – sbi Sep 21 '10 at 19:48

6 Answers6

50

This is called the placement new operator. It allows you to supply the memory the data will be allocated in without having the new operator allocate it. For example:

Foo * f = new Foo();

The above will allocate memory for you.

void * fm = malloc(sizeof(Foo));
Foo *f = new (fm) Foo(); 

The above will use the memory allocated by the call to malloc. new will 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 to new.

A 'gotcha' for placement new is that you should not release the memory allocated by a call to the placement new operator using the delete keyword. You will destroy the object by calling the destructor directly.

f->~Foo();

After the destructor is manually called, the memory can then be freed as expected.

free(fm);
linuxuser27
  • 7,183
  • 1
  • 26
  • 22
  • 2
    Yep. See also [this answer](http://stackoverflow.com/questions/2697892/what-is-return-type-of-new-in-c/2697929#2697929) for the difference between a _new expression_ and actual memory allocation. – sbi Sep 21 '10 at 19:50
  • 3
    "`new` will not allocate any more." - for the object as a fixed-sized thing - sure but of course it will allocate dynamic memory if the object requires it - strings, framebuffers, databases, whatever. – Euri Pinhollow Dec 30 '16 at 20:37
  • Is the destructor allowed to use free() to deallocate the memory allocated to the object through malloc? I am assuming the answer is yes. However, Is there a catch to this? – thegreatcoder Oct 07 '18 at 18:47
  • @thegreatcoder Yes. that is what should happen. I will update the answer. – linuxuser27 May 04 '19 at 22:31
  • 2
    Make sure to include header ``! – Czipperz Dec 12 '19 at 18:27
7

The short answer is that your code constructs an object in the space pointed to by y. The long answer is best covered by the C++ FAQ.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
4

This is more commonly known as 'placement new' and is discussed pretty well by the C++ FAQ (in the 'Destructors' area):

It allows you to construct objects in raw memory, which can be useful in certain specialized situations, such as when you might want to allocate an array for a large number of possible objects, but want to construct then as needed because you often might not need anywhere near the maximum, or because you want or need to use a custom memory allocator.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
2

I'm rusty on this one but it allows you to write the object to a memory block you have already allocated. It also needs a reciprocal delete statement to clear it from memory.

wheaties
  • 35,646
  • 15
  • 94
  • 131
2

If you use a memory pool, then you need to use the in place constructor to initialize your object as they are allocated from the pool.

stonemetal
  • 6,111
  • 23
  • 25
1

It's a way to call a constructor without allocating memory. Your y has to be a pointer poniting to enough memory for a new Datatype object. Also, don't call delete, call ~DataType().

Fozi
  • 4,973
  • 1
  • 32
  • 56