This is a C++ writeup.

Placement new is an odd feature. When you create an object with regular ordinary operator new, memory is allocated and, if the object is a class instance, the constructor (if any) is called1.

That's fine as far as it goes, but it turns out not to go as far as we might like. For example, it is sometimes handy and occasionally necessary to create a new class instance at one particular address in memory: Sometimes it's not enough just to know that it's off on the heap somewhere. By way of an example in The Design and Evolution of C++1, Bjarne Stroustrup mentions how special hardware may need something to be at a particular address. Stroustrup also tells us that we might want to ensure that an object is in some particular "arena" in memory -- we wouldn't care about the exact address, just that it's in a particular area, for example in the shared memory of a multi-processor system. In that case, a class-specific overload of operator new won't do the job, because you might want different instances of the same class to be in particular different places.

Another and more commonly useful use is to create and destroy objects without having to reallocate memory. STL containers make use of this; note that they all take allocators as default arguments. You can do things more efficiently if you get to make all the choices yourself (iff you know what you're doing).

So, when you get right down to it, it's not so wise to think of new as nothing but a memory allocation routine like malloc. Think of it as a thing that calls constructors, and/or allocates memory. It can also so all manner of other things, once you're defining it yourself.

A placement new operator (you have to define your own) is used like this (the example doesn't do anything worth doing; it just illustrates the syntax):

void * mem = (void *)0xABCD; //  An arbitrary address that the hardware likes.
foo *  p   = new(mem) foo;   //  This puts an initialized foo instance at 
                             //  0xABCD; mem is an argument to new().

A placement new operator looks like this. For the first argument, you get the size of the object being allocated; that part happens automagically. The operator is overloaded according to the type of the second argument, which can be anything: Storage, an allocator, whatever you like. The return type is always void *, but don't worry about that. new is a special case.

//  This is like the one used above.
void *operator new( size_t size, void *p )
    return p;

//  This one is a bit more clever; assume any old arbitrary 
//  allocator class.
void *operator new( size_t size, allocator &a )
    return a.alloc( size );

I seem to recall that this feature was one of the features added at the request of Alexander Stepanov for the sake of the Standard Template Library, but I'm not sure when. At the time the ANSI C++ standard was finalized (1998?), I recall Al Stevens mentioning placement new in his column in Dr. Dobb's Journal, and I'm pretty sure he spoke of it as a new feature. I wish I kept my back issues in some kind of order other than an unruly mound. Anyhow, it's mentioned in the second edition of The C++ Programming Language2 (1995) right there on page 214, so it's at least that old. The Design and Evolution of C++3 tells us what and why, but not when.

1Well, if any members have constructors of their own, the compiler will give the thing a default constructor of sorts if the programmer hasn't bothered with one. The generated one will just call the default constructors of the members.

2By Bjarne Stroustrup. Compare to K&R. It doesn't compare so favorably.

3By Bjarne Stroustrup. It's a book well worth reading if you use C++ or care about C++ -- and oh, very much especially if you oppose C++.

Log in or register to write something here or to contact authors.