title: Mysterious error in this C++ pointer program in visual studio
body: <p>I have a written code for finding missing elements below.</p>
  <pre><code>#include &lt;iostream&gt;
  
  int main() {
      int arr[7] = { 1,3,5,7,9,11,13 };
      int max{ arr[0] }, min{ arr[0] };
      for (int i = 0; i &lt; 7; i++) {
          if (max &lt; arr[i])
              max = arr[i];
          if (min &gt; arr[i])
              min = arr[i];
      }
      int* ptr = new int[max];
      for (int j = 0; j &lt; 7; j++)
          ptr[arr[j]] = 1;
      
      for (int k = min; k &lt; max; k++)
          if (ptr[k] == 0)
              std::cout &lt;&lt; k &lt;&lt; &quot; &quot;;
  
      delete[] ptr;
  
      return 0;
  }
  </code></pre>
  <p>However this gives me an mysterious error about heap corruption like this :</p>
  <p><a href="https://i.stack.imgur.com/wQ2uF.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>I don't see any problem here. Anyone see it ? Please tell me.</p>