Placement syntax: Difference between revisions

Content deleted Content added
Have some citation templates, and a new citation as well.
Have a Gotcha!
Line 154:
</source>
 
The latter would involve simply writing the destructor invocation and delete function call into the program:<ref name=Vermeir1 /><ref name=Dewhurst>{{cite book|title=C++ Gotchas|author=Stephen C. Dewhurst|chapter=Gotcha #62: Replacing Global New and Delete|chapterurl=Gotcha #62: Replacing Global New and Delete|pages=173&ndash;176|publisher=Addison-Wesley|date=2003|isbn=9780321125187}}</ref>
:<source lang="cpp" enclose=div>
A arena ;
T * p = new (arena) T ;
/* ... */
p->~T() ; // First invoke the destructor explicitly.
operator delete(p, arena) ;
operator delete(p, arena) ; // Then call the deallocator function indirectly via operator delete(void *, A &) .
</source>
 
A common error is to attempt to use a delete expression to delete the object. This results in the wrong <code>operator delete</code> function being called. Dewhurst recommends two strategies for avoiding this error. The first is to ensure that any custom allocators rely upon the Standard C++ library's global, non-placement, <code>operator new</code>, and are thus nothing more than simple wrappers around the C++ library's memory management. The second is to create new and delete functions for individual classes, and customize memory management via class function members rather than by using the placement syntax.<ref name=Dewhurst />
 
=== Debugging ===