title: Should I delete a pointer if it is a function arguemente and is being passed values by reference?
body: <p>I'm sure this question has been answered some where, and please link me to it if it has, but I'm struggling to find an answer to this exact question. There are many that are very close. Here are some I've already read that don't quite answer my question:</p>
  
  <p><a href="https://stackoverflow.com/questions/4776010/pass-by-reference-and-value-with-pointers">pass by reference and value with pointers</a>
  <a href="https://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c">Reason to Pass a Pointer by Reference in C++?</a>
  <a href="https://stackoverflow.com/questions/1881681/using-delete-on-pointers-passed-as-function-arguments">Using delete on pointers passed as function arguments</a></p>
  
  <p>Here is a basic outline of the situation, trying to make it pretty close to my actual usage:</p>
  
  <pre><code>void func (vector&lt;int&gt;* foo) {
  
      int bar = foo-&gt;at(0);
  
      // transform bar
  
      foo-&gt;at(0) = bar;
  
      // Should I delete foo?
      delete foo;
  }
  
  int main() {
  
      vector&lt;int&gt; data (4, 0);
  
      data.at(0) = 5;
  
      for (int i = 0; i &gt; 10; i++) {
  
          func(&amp;data);
  
      }
  }
  </code></pre>
  
  <p>edit: the for loops above should read like this:</p>
  
  <pre><code>for (int i = 0; i &lt; 10; i++) {
  </code></pre>
  
  <p>So, as you can see, I have created a vector called data, and then passed a reference of data to the function. The function gets a value from data via the reference, transforms it, and then saves the new value back to data again via the reference.</p>
  
  <p>In this specific scenario, this function is the only function that receives this data, but it gets called repeatedly and passed the reference to the same data each time.</p>
  
  <p>My main question is, should I delete foo at the end of that function? Also, if I had an extra function also being passed a reference to data, would that change the answer?</p>
  
  <p>I have read other questions that suggest to be careful deleting a pointer in a function, but they have all related to a data type that was declared as a pointer in the first place. I am not declaring a pointer or reference in the first place, just creating a reference as an argument to the function (I think?).</p>
  
  <p>The reason I ask this anyway, is that my program has some serious memory leaks. I've done some research, and read lots about pointer related memory leaks, and it's occurred to me this might be one source of it</p>
  
  <p>I have tried adding a "delete" at the end of one such function, and all seems well, so far... but I don't want to add a whole bunch of deletes, then some time down the road find out that it was a really bad idea :D</p>
  
  <p>Cheers in advance, Pete</p>