title: Narrow scope of a variable and performance?
body: <p>I have a <code>main</code> method looks like this.</p>
  <pre class="lang-c prettyprint-override"><code>    struct list *l = array_list();
      if (l == NULL) {
          return EXIT_FAILURE;
      }
      srand(time(NULL));
      int size = 4;
      for (int i = 0; i &lt; size; i++) {
          int *d = malloc(sizeof(int));
          *d = rand();
          list_insert_last(l, d);
          printf(&quot;inserted: %d\n&quot;, *d);
      }
      printf(&quot;size: %zu\n&quot;, list_size(l));
      for (int i = 0; i &lt; size; i++) {
          int *d = list_delete_first(l);
          printf(&quot;deleted: %d\n&quot;, *d);
          free(d);
      }
      printf(&quot;size: %zu\n&quot;, list_size(l));
      array_list_free(l);
      return EXIT_SUCCESS;
  </code></pre>
  <p>My question is, even though I already tagged <code>premature-optimization</code>, about the <code>int *d</code> variable.</p>
  <p>Will it be better declaring the variable once out of loops and reuse it?</p>
  <pre class="lang-c prettyprint-override"><code>    struct list *l = array_list();
      if (l == NULL) {
          return EXIT_FAILURE;
      }
      srand(time(NULL));
      int size = 4;
      int *d; // declare once
      for (int i = 0; i &lt; size; i++) {
          d = malloc(sizeof(int)); // reuse it
          *d = rand();
          list_insert_last(l, d);
          printf(&quot;inserted: %d\n&quot;, *d);
      }
      printf(&quot;size: %zu\n&quot;, list_size(l));
      for (int i = 0; i &lt; size; i++) {
          d = list_delete_first(l); // reuse it
          printf(&quot;deleted: %d\n&quot;, *d);
          free(d);
      }
      printf(&quot;size: %zu\n&quot;, list_size(l));
      array_list_free(l);
      return EXIT_SUCCESS;
  </code></pre>