Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the example code, the destructor for <code>mystruct</code> is called at the end of <code>main</code>.</p> <p><strong>Pointers</strong> When a pointer falls out of scope, nothing happens to the object being pointed to. The lifetime of the object stays whatever it would have been had the pointer never existed. There is, of course, an important detail: objects created by <code>new</code>/<code>new[]</code> exist until they are <code>delete</code>d/<code>delete[]</code>d, and memory allocated by <code>malloc</code> exists until it's <code>free</code>d.</p> <p><strong>References</strong> A reference is nothing more than an additional name for an existing object. With the exceptions of a <code>const</code> reference pointing at a temporary object, the reference's lifetime has no effect on the object-being-referred-to's-lifetime; the destructor is called when that object falls out of scope normally (a <code>const</code> reference to a temporary object keeps the temporary alive until the <code>const</code> reference falls out of scope).</p> <p><a href="https://stackoverflow.com/a/4715888/103167">Using the term "object" is a simplification</a>, the pointer or reference really points at memory; it doesn't "track" the actual object in any way. For instance, it is possible to destroy an object being pointed to before the pointer falls out of scope, or to destroy an object being referenced before the reference falls out of scope. These invalid pointers/references are called "dangling pointers" and "dangling references" respectively:</p> <pre><code>int* i = new int(5); int&amp; j = *i; delete i; // i is now a dangling pointer, and j is now a dangling reference </code></pre>
    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.
 

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