title: How to delete the new array in c++?
body: <p>I have created a new point function in c++, where I create a new array by Dynamic memory allocation. However, when I want to delete the array in main function, it reports errors like:</p>
  
  <pre><code>'Assessment_1.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\ucrtbased.dll'
  'Assessment_1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 
  The thread 0x22dc has exited with code 0 (0x0).
  HEAP[Assessment_1.exe]: Invalid address specified to RtlValidateHeap( 00050000, 0005F25C )
  Assessment_1.exe has triggered a breakpoint.
  </code></pre>
  
  <p>The return_array.cpp contains the function in which user inputs the keyboards.</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  #include &lt;conio.h&gt;
  using namespace std;
  //const static int length = 3;
  
  int* input() {
  
      //static int number[length];  fixed length for input 
  
      static int* number_array;
      int number_length = 30;
      int index = 0;
      number_array = new int[number_length];
      cout &lt;&lt; "enter (ESC) to escape the program!" &lt;&lt; endl;
  
      while (_getch() != 27)
      {
          cout &lt;&lt; "Input your number " &lt;&lt; index &lt;&lt; " elment: " &lt;&lt; endl;
          cin &gt;&gt; number_array[index];
          index = index + 1;
          cout &lt;&lt; endl;
      }
  
      return number_array;
  }
  </code></pre>
  
  <p>The practice.cpp contains the main function and the evaluation function of two vectors. The bug is when I add the <strong><em>delete []vector_1;</em></strong>. </p>
  
  <pre><code>#include &lt;vector&gt;
  #include&lt;iostream&gt;
  #include"myFunctions.h"
  using namespace std;
  const static int length = 3;
  bool same_vec(vector&lt;int&gt; a, vector&lt;int&gt; b);
  
  void main() {
      vector&lt;int&gt; new_vector_1(length);
      vector&lt;int&gt; new_vector_2(length);
  
      int* vector_1 = input();
  
      for (int i = 0; i &lt; length; i++) {
          new_vector_1[i] = *vector_1;
          cout &lt;&lt; *vector_1 &lt;&lt; endl;
  
          vector_1 = vector_1 + 1;
      }
      delete []vector_1;
  
      for (int i = 0; i &lt; length; i++) {
          cout &lt;&lt; " The result of vector_1: " &lt;&lt; new_vector_1[i] &lt;&lt; endl;
      }
  
      int* vector_2;
      vector_2 = input();
  
      for (int i = 0; i &lt; length; i++) {
          new_vector_2[i] = *vector_2;
          vector_2 = vector_2 + 1;
      }
      delete []vector_2;
  
      for (int i = 0; i &lt; length; i++) {
  
          cout &lt;&lt; " The result of vector_2: " &lt;&lt; new_vector_2[i] &lt;&lt; endl;
      }
  
      bool qax = same_vec(new_vector_1, new_vector_2);
  
      if (qax == false) {
          cout &lt;&lt; "the items are not match!"&lt;&lt;endl;
      }
      else {
          cout &lt;&lt; "the items are match!" &lt;&lt; endl;
      }
  
  }
  
  bool same_vec(vector&lt;int&gt; a, vector&lt;int&gt; b) {
      //Evaluate the elements in the two vectors
  
      bool flag = true;
      int length_a = a.size();
      int length_b = b.size();
  
      vector&lt;bool&gt; new_bool(length_a);
  
      for (int i = 0; i &lt; length_a; i++) {
          for (int j = 0; j &lt; length_b; j++) {
              if (a[i] == b[j]) {
                  new_bool[i] = true;
                  cout &lt;&lt; a[i] &lt;&lt; " " &lt;&lt; b[j] &lt;&lt; endl;
                  break;
              }
          }
      }
  
      for (int i = 0; i &lt; new_bool.size(); i++) {
          if (new_bool[i] == false) {
              flag = false;
          }
      }
  
      return flag;
  }
  </code></pre>
  
  <p>And the myFunction.h is my head file.</p>
  
  <pre><code>#pragma once
  int* getRandom();
  
  int* input();
  </code></pre>
  
  <p>Can someone helps to solve the problem? I know one solution is delete the line of <strong>delete []vector_1;</strong>. The solution may cause memory-leak. </p>