Placement syntax: Difference between revisions

Content deleted Content added
Capitalization
Line 77:
</source>
 
placement new is used when you do not want operator new to allocate memory (you have pre-allocated it and you want to place the object there), but you do want the object to be constructed. examplesExamples of typical situations where this may be required are:
a.* youYou want to create objects in memory shared between two different processes.
b.* youYou want objects to be created in non-pageable memory.
c.* youYou want to separate memory allocation from construction e.g. in implementing a std::vector<> (see std::vector<>::reserve).
 
The basic problem is that the constructor is a peculiar function; when it starts off, there is no object, only raw memory. And by the time it finishes, you have a fully initialized object. Therefore i) The constructor cannot be called on an object ii) However, it needs to access (and initialize) non-static members. This makes calling the constructor directly an error. The solution is the placement form of operator new.