Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The syntax of <code>delete</code> is a bit misleading. When you write</p> <pre><code>T* ptr = /* ... */ delete ptr; </code></pre> <p>You are <strong>not</strong> deleting the variable <code>ptr</code>. Instead, you are deleting the object that <code>ptr</code> points at. The value of <code>ptr</code> is unchanged and it still points where it used to, so you should be sure not to dereference it without first reassigning it.</p> <p>There is no requirement that you <code>delete</code> a pointer before you reassign it. However, you should ensure that if you are about to reassign a pointer in a way that causes you to lose your last reference to the object being pointed at (for example, if this pointer is the only pointer in the program to its pointee), then you should <code>delete</code> it to ensure that you don't leak memory.</p> <p>One technique many C++ programmers use to simplify the logic for when to free memory is to use <a href="http://en.wikipedia.org/wiki/Smart_pointer" rel="nofollow">smart pointers</a>, objects that overload the operators necessary to mimic a pointer and that have custom code that executes automatically to help keep track of resources. The new C++0x standard, for example, will provide a <code>shared_ptr</code> and <code>unique_ptr</code> type for this purpose. <code>shared_ptr</code> acts like a regular pointer, except that it keeps track of how many <code>shared_ptr</code>s there are to a resource. When the last <code>shared_ptr</code> to a resource changes where it's pointing (either by being reassigned or by being destroyed), it then frees the resource. For example:</p> <pre><code>{ shared_ptr&lt;int&gt; myPtr(new int); *myPtr = 137; { shared_ptr&lt;int&gt; myOtherPtr = myPtr; *myPtr = 42; } } </code></pre> <p>Notice that nowhere in this code is there a call to <code>delete</code> to match the call to <code>new</code>! This is because the <code>shared_ptr</code> is smart enough to notice when the last pointer stops pointing to the resource.</p> <p>There are a few idiosyncrasies to be aware of when using smart pointers, but they're well worth the time investment to learn about. You can write much cleaner code once you understand how they work. </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. 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.
    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