title: Best practices for variable scopes with vectors
body: <p>My understanding is that C++ best practice is to define variables with the smallest scope possible. </p>
  
  <p>My understanding is that the primary reason for this is that it will help prevent accidental reuse. In addition, there almost never a performance hit for doing so (or so I have been told). To the contrary, people seem to indicate that a compiler may actually be able to produce equivalent or better code when variables are defined locally. For example, the following two functions produce the same binaries <a href="https://godbolt.org/z/tv-Eas" rel="nofollow noreferrer">in Godbolt</a>:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  
  void printRand1() {
      char val;
      for (size_t i = 0; i &lt; 100 ; ++i) {
          val = rand();
          puts(&amp;val);
      }
  }
  
  void printRand2() {
      for (size_t i = 0; i &lt; 100 ; ++i) {
          const char val = rand();
          puts(&amp;val);
      }
  }
  </code></pre>
  
  <p>So in this case, version 2 is clearly preferable. This context I can totally agree with and understand. </p>
  
  <p>What is not clear to me is whether the same logic should be applied to larger data types such as arrays or vectors. One particular thing that I find a lot in code is something like this: </p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  struct Bob {
  
      std::vector&lt;char&gt; buffer;
  
      void bar(int N) {
          buffer.resize(N);
          for (auto &amp; elem : buffer) {
              elem = rand();
              puts(&amp;elem);
          }
      }
  };
  
  void bob() {
      Bob obj;
      obj.bar(100);
  }
  </code></pre>
  
  <p>despite the fact that we could have localized the data better in this dumb example:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  struct Bob {
  
      void bar(int N) {
          std::vector&lt;char&gt; buffer(N);
          for (auto &amp; elem : buffer) {
              elem = rand();
              puts(&amp;elem);
          }
      }
  };
  
  void bob() {
      Bob obj;
      obj.bar(100);
  }
  </code></pre>
  
  <blockquote>
    <p>Note: Before you guys jump on this, I totally realize that you don't actually need a vector in this example. I am just making a stupid example so that the binary code is not too large on Godbolt. </p>
  </blockquote>
  
  <p>The rationale here for NOT localizing the data (aka. Snippet 1) is that the buffer could be some large vector, and we don't want to keep reallocating it every time we call the function. </p>
  
  <p>The rationale for Snippet 2 is to localize the data better. </p>
  
  <p>So what logic should I apply for this scenario? I am interested in the case where you actually need a vector (in this case you don't). </p>
  
  <p>Should I follow the localization logic? or should I ride by the logic that I should try to prevent repeated reallocations? </p>
  
  <p>I realize that in an actual application, you would want to benchmark the performance, not the compiled size in Godbolt. But I wonder what should be my default style for this scenario (before I start profiling the code).</p>