Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>What is happening</strong></p> <p>When you write <code>T t;</code> you're creating an object of type <code>T</code> with <em>automatic storage duration</em>. It will get cleaned up automatically when it goes out of scope.</p> <p>When you write <code>new T()</code> you're creating an object of type <code>T</code> with <em>dynamic storage duration</em>. It won't get cleaned up automatically.</p> <p><img src="https://i.stack.imgur.com/Rn5g3.png" alt="new without cleanup"></p> <p>You need to pass a pointer to it to <code>delete</code> in order to clean it up:</p> <p><img src="https://i.stack.imgur.com/I01Fx.png" alt="newing with delete"></p> <p>However, your second example is worse: you're dereferencing the pointer, and making a copy of the object. This way you lose the pointer to the object created with <code>new</code>, so you can never delete it even if you wanted!</p> <p><img src="https://i.stack.imgur.com/hlrGB.png" alt="newing with deref"></p> <p><strong>What you should do</strong></p> <p>You should prefer automatic storage duration. Need a new object, just write:</p> <pre><code>A a; // a new object of type A B b; // a new object of type B </code></pre> <p>If you do need dynamic storage duration, store the pointer to the allocated object in an automatic storage duration object that deletes it automatically.</p> <pre><code>template &lt;typename T&gt; class automatic_pointer { public: automatic_pointer(T* pointer) : pointer(pointer) {} // destructor: gets called upon cleanup // in this case, we want to use delete ~automatic_pointer() { delete pointer; } // emulate pointers! // with this we can write *p T&amp; operator*() const { return *pointer; } // and with this we can write p-&gt;f() T* operator-&gt;() const { return pointer; } private: T* pointer; // for this example, I'll just forbid copies // a smarter class could deal with this some other way automatic_pointer(automatic_pointer const&amp;); automatic_pointer&amp; operator=(automatic_pointer const&amp;); }; automatic_pointer&lt;A&gt; a(new A()); // acts like a pointer, but deletes automatically automatic_pointer&lt;B&gt; b(new B()); // acts like a pointer, but deletes automatically </code></pre> <p><img src="https://i.stack.imgur.com/TLUl4.png" alt="newing with automatic_pointer"></p> <p>This is a common idiom that goes by the not-very-descriptive name RAII (<em>Resource Acquisition Is Initialization</em>). When you acquire a resource that needs cleanup, you stick it in an object of automatic storage duration so you don't need to worry about cleaning it up. This applies to any resource, be it memory, open files, network connections, or whatever you fancy.</p> <p>This <code>automatic_pointer</code> thing already exists in various forms, I've just provided it to give an example. A very similar class exists in the standard library called <code>std::unique_ptr</code>.</p> <p>There's also an old one (pre-C++11) named <code>auto_ptr</code> but it's now deprecated because it has a strange copying behaviour.</p> <p>And then there are some even smarter examples, like <code>std::shared_ptr</code>, that allows multiple pointers to the same object and only cleans it up when the last pointer is destroyed.</p>
    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