title: Will an object defined inside a C++ function be destroyed after it goes out of scope?
body: <p>I've defined an object inside a function:</p>
  
  <pre><code>SoundFXSystem* Engine::GetSystem(){
     SoundFXSystem system; // var defined inside a function
     sEngine-&gt;GetSystem(&amp;system);
     return &amp;system; // can I return the address of the var?
  }
  </code></pre>
  
  <p>Is it legal to return the address of the var and use it later in the program? Will it go out of scope immediately as the function exits and be unavailable for use by the caller?</p>
  
  <p><strong>Edit:</strong> If I define the object as follows, will it be dynamically allocated?</p>
  
  <pre><code>SoundFXSystem* Engine::GetSystem(){
     SoundFXSystem* system = new SoundFXSystem(); // var defined inside a function
     sEngine-&gt;GetSystem(&amp;system);
     return system; 
  }
  </code></pre>