In “The C++ Programming Language” book, in list of operations (article 6.2), Bjarne Stoustrup wrote this:
create (place) new ( expr-list ) type
create (place and initialize) new ( expr-list ) type ( expr-list )
I’ve never heard about this kind of the new operator, and I’m interested what does it do.
This is called placement-new. You can create an object over already existing memory.
Here’s an explanation and an useful question in SO
You can also have
nothrow, for example:which tells, that
newwill not throwstd::bad_allocin case of out of memory, but it will returnNULL, instead.Another example, that comes to my mind – the standard containers (probably) use
placement new: when you callreserve, memory is allocated, but nothing is constructed/initialzed on this memory. So, when you insert (withstd::vector<T>::push_backfor example), if there’s allocated, but not initialized memory –placement newis used.