title: Is it advised to declare variables inside loops?
body: <p>As far as I know, declaring a variable inside a loop is less efficient than declaring outside and modify it inside the loop.</p>
  <p>Example:</p>
  <pre><code>std::list&lt;&lt;double&gt; l;
  for(int i = 0; i &lt; 100000; ++i)
  {
      double a;
      a = 500.0 * i;
      l.append(a);
  }
  </code></pre>
  <p>Another example with pointer:</p>
  <pre><code>std::list&lt;&lt;double *&gt; l;
  for(int i = 0; i &lt; 100000; ++i)
  {
      double* a;
      *a = 500.0 * i;
      l.append(a);
  }
  </code></pre>
  <p>The examples don't have enough sense but I just want to show that the double and the pointer are being declared inside the loop.</p>
  <p>The thing is, the scope of the variable is the same as the loop, so when the loop do an iteration, will it destroy the variable and then declaring it again? Or it just stays in the heap until the end of the <code>for</code> loop? How efficient is it to do that? Is it a waste of resources?</p>
  <p>I code it as it was in C++.</p>
  <p>Thanks in advance!</p>