title: When is a vector copied, when is a reference passed?
body: <p>I'm working with a program that makes use of <code>std::vector</code> a lot. Also there are lots of allocations/deallocations taking place, billions of them, and I'm trying to avoid as many of them as possible.
  Since I'm relatively new to C++, I have a few questions regarding allocations taking place when using a vector (e.g. when adding elements to it).
  I'm on a Win7 64-bit machine, the program is 32-bit and I'm using the current version of MinGW-compilers.</p>
  
  <p>I'd like to know, what happens in the following cases, i.e. if the vector is copied, passed as a reference, ...</p>
  
  <p>1.</p>
  
  <pre><code>std::vector&lt;T&gt; fillVector() {
      std::vector&lt;T&gt; returnVector;
      ...
      return returnVector;
  }
  
  std::vector&lt;T&gt; myVector = fillVector();
  </code></pre>
  
  <p>2.</p>
  
  <pre><code> std::vector&lt;T&gt;&amp; fillVector() {
      std::vector&lt;T&gt;* returnVector = new std::vector&lt;T&gt;;
      ...
      return (*returnVector);
  }
  
  std::vector&lt;T&gt; myVector = fillVector();
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>std::vector&lt;T&gt;* fillVector() {
      std::vector&lt;T&gt;* returnVector = new std::vector&lt;T&gt;;
      ...
      return returnVector;
  }
  
  std::vector&lt;T&gt;* myVector = fillVector();
  </code></pre>
  
  <p>And the following, different operations:</p>
  
  <p>4.</p>
  
  <pre><code>std::vector&lt;T&gt; myVector1;
  ... (myVector1 being filled)
  std::vector&lt;T&gt; myVector = myVector1;
  </code></pre>
  
  <p>5.</p>
  
  <pre><code>std::vector&lt;T&gt;* myVector1 = new std::vector&lt;T&gt;;
  ... (myVector1 being filled)
  std::vector&lt;T&gt; myVector = (*myVector1);
  </code></pre>
  
  <p>Assuming I don't want to change the argument in <code>myFunction</code>/changes in to <code>myVector</code> in <code>myFunction</code> wouldn't hurt the rest of the program:</p>
  
  <p>6.</p>
  
  <pre><code>void myFunction(std::vector&lt;T&gt; myParam) {
     ...
  }
  
  std::vector&lt;T&gt; myVector;
  ... (myVector being filled)
  myFunction(myVector);
  </code></pre>
  
  <p>7.</p>
  
  <pre><code>void myFunction(std::vector&lt;T&gt;&amp; myParam) {
     ...
  }
  
  std::vector&lt;T&gt; myVector;
  ... (myVector being filled)
  myFunction(myVector);
  </code></pre>
  
  <p>If my understanding is correct, the fastest options (meaning passing references instead of creating copies and passing them) would be 2/3, 5 and 7. Please correct me if I'm wrong!</p>