Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>when that function calls, end of function that local variable x will be what?</p> </blockquote> <p>Nonexistent. Evaportated. Vapor. Gone.</p> <p><code>x</code> here is a variable with automatic lifetime. "Automatic" refers to the fact that when <code>x</code> goes out of scope, it will be destroyed. (Not exactly <em>deleted</em>, as that term implies calling <code>delete</code>)</p> <p>The same is true of all automatic variables, wherether they be integrals, strings, floats, or even pointers:</p> <pre><code>void f2() { int a=100; int* b=&amp;a; } </code></pre> <p>Here both <code>a</code> and <code>b</code> are automatic variables. They will both be destroyed at the end of their scope -- the end of <code>f2()</code>. However, all that will be destroyed is the varible itself. If an automatic variable is a pointer (eg, a <em>raw</em> pointer), then the <em>thing the pointer points to</em> will <strong>not</strong> be destroyed. Consider:</p> <pre><code>void f3() { char* p = new char [256]; } </code></pre> <p><code>p</code> here is still an automatic variable, of type pointer-to-char. The thing <code>p</code> points to is not automatic -- that is dynamically allocated (we used <code>new</code>).</p> <p>As written above, <code>p</code> will be destroyed but the memory it points to will not. This is a memory leak. In order to resolve this, you must <code>delete</code> it:</p> <pre><code>void f3() { char* p = new char [256]; delete [] p; } </code></pre> <p>Now the memory pointed to by <code>p</code> has been correctly destroyed, and the pointer itself (<code>p</code>) will be destroyed at the end of its scope since it's an automatic.</p> <p>The lifetime of <code>p</code> and what <code>p</code> points to are not connected in any way. Just because you <code>delete</code> <code>p</code> doesn't mean that <code>p</code> <em>itself</em> is now also destroyed. <code>p</code> is still an automatic variable.</p> <p>An example:</p> <pre><code>void f3() { char* p = new char [256]; cout &lt;&lt; (void*) p &lt;&lt; "\n" delete [] p; // p still exists, it just points to to nothing useable cout &lt;&lt; (void*) p &lt;&lt; "\n" } </code></pre> <p>After the <code>delete</code>, we print the value of <code>p</code> to the screen. If you run this, you'll see that the value of <code>p</code> doesn't change from before deleteing it. <code>p</code> itself was unaffected by <code>delete [] p;</code>. But at <code>f3()</code>'s closing brace <code>}</code>, it will fall out of scope and there be 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