title: How to prevent unnecessary memory use in recursive functions
body: <p>I've just written a recursive function and it dawned on me that all the variables I use within the function will remain allocated in memory until recursion breaks.  If I am recursing a large number of times or allocating large amounts of memory for variables not used in the consequent recursive function call, could this lead to alot of wasteful memory use?</p>
  
  <p>E.g. in the following, only <code>vec2</code> is used in the following recurse and <code>temp_int</code> and <code>temp_vec</code> will continue to occupy memory needlessly. </p>
  
  <pre><code>int recurse(std::vector&lt;int&gt; arg_vec) {
    int temp_int i;
  
    std::vector&lt;int&gt; temp_vec;
    std::vector&lt;int&gt; vec2;
  
    //... do some processing with arg_vec and temp_vec and result is stored in vec2
    recurse(vec2)
  
    return if (some condition met);
  }
  </code></pre>
  
  <p>Should I then be allocating all memory using the new commands and deleting them before the function call?  Or is there some other method for dealing with this</p>