title: How to delete an array that a function returns
body: <p>I am new to C++ and I'm having a small memory leak issue. The structure of my program is very simple, as shown below, although I have left out parts that are irrelevant to the problem.</p>
  
  <pre><code>int** foo(int sizeOfArray){
  
       int** arrayToReturn = new int*[sizeOfArray];
  
       for (int i = 0; i &lt; sizeOfArray; i++) {
             arrayToReturn[i] = new int[2];
       }
       return arrayToReturn;
  }
  
  int main() {       
      a = someNumber;                // 'a' can be any value ( &lt; 64)
      int** someArray = foo(int a);
      // Do stuff with someArray
  }
  </code></pre>
  
  <p>So essentially, it calls a function that returns an array of arrays. My problem is that when my program calls this function thousands of times, they cause a memory leak because since I am using NEW, I need to use delete. I'm just not sure how to delete the arrays, as I need to use them in the main function so I can't delete them within the function foo, but how do I delete them afterwards? I tried just deleting <code>someArray</code>, but the memory used up from the line <code>arrayToReturn[i] = new int[2]</code> is never freed.</p>
  
  <p>Hopefully that makes sense, I am sorry if it's a stupid question, and thanks for reading!</p>