Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You really need to do <code>b_ptr = d_ptr</code> after placement-new of <code>Derived</code>, in case the <code>Base</code> subobject isn't first in the layout of <code>Derived</code>. As written, <code>b_ptr-&gt;g(4)</code> evokes undefined behavior.</p> <p>The rule (3.8 <code>basic.life</code>):</p> <blockquote> <p><strong>If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object</strong>, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, <strong>can be used to manipulate the new object, if</strong>:</p> <ul> <li>the storage for the new object exactly overlays the storage location which the original object occupied, and</li> <li>the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and</li> <li>the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and</li> <li>the original object was a most derived object (1.8) of type <code>T</code> and the new object is a most derived object of type <code>T</code> (that is, they are <strong>not base class subobjects</strong>).</li> </ul> </blockquote> <p>You also should probably be destroying the old object before reusing its memory, but the standard doesn't mandate this. However failure to do so will leak any resources owned by the old object. The full rule is given in section 3.8 (<code>basic.life</code>) of the Standard:</p> <blockquote> <p>A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and <strong>any program that depends on the side effects produced by the destructor has undefined behavior</strong>.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload