TITLE: using placement delete (Newsgroups: comp.std.c++, 27 Aug 97) ISJ: isj@image.dk > I do not completely understand how placement works on operator delete. > > The December '96 Working Paper contains a lot of references to > "placement operator delete" in 3.7.3.2, 5.3.4(.18), 15.2(.2) and > 18.4.1.3(.2) and explains much about them (parameters, exceptions, > prototype etc..). But I cannot find a single example of calling an > placement operator delete. > > The grammar of delete-expression in 28.4 does not allow a > "delete-placement" (neither does the grammar in "C++ prog.lang." 3rd > ed.). CLAMAGE: Steve Clamage Let's be sure we are not confusing a delete-expression, as in delete p; with a function whose name is "operator delete". There is no delete-expression which will invoke a placement form of operator delete. You can still call the function explicitly. Example: // a pair of placement operators void operator delete(void*, int); void* operator new(size_t, int); T* p = new (12) T; // invoke placement new ... // destroy object and delete space manually p->~T(); operator delete(p, 12); If you just wrote delete p; it would use the normal operator delete, which presumably is not correct, since you took the trouble to declare a form to match the placment new. The placement operator delete is called automatically ONLY in the case when a constructor exits via an exception and was invoked by using a placement expression. For example, suppose the constructor for T above exited via an exception. Since there is no way for the programmer to know how much got constructed, the compiler is required to generate code that automatically destroys the already- constructed parts and deletes the allocated space. If there is a placement form of operator delete that matches the placement operator new, it is called. Otherwise, the normal operator delete is called. Only local information is needed for the compiler to be able to take care of the situtation. The compiler generates code to trap an exception and pass the same extra values to placement operator delete that it passed to placement operator new. ISJ: > ... It is also more-or-less > contradicted by "C++ prog.lang." page 256, section 10.4.11 ... CLAMAGE: The 2nd edition of C++PL was written before placement delete was added to the language definition. The example you cite is still valid. Since placement delete was not available, a more complicated solution was used than is now needed.