title: C++ Deleting part of dynamic array
body: <p>Say I have a dynamic array like:</p>
  
  <pre><code>int* integers = new int[100];
  </code></pre>
  
  <p>Is there a way to delete only part of the array such as:</p>
  
  <pre><code>int* integers2 = integers + 50;
  delete[] integers2;
  </code></pre>
  
  <p>I want to not only delete everything 50 and beyond, but if I call another delete[] on the original integers array, then it would only delete the correct amount of memory and not try to delete the originally allocated amount and seg fault.</p>
  
  <p>Why I want to do this: I have a data structure that is structured in levels of arrays, and I want to be able to create this data structure from a full array.  So I want to be able to say</p>
  
  <pre><code>int* level1 = integers;
  int* level2 = integers + 50;
  int* level3 = integers + 100;
  </code></pre>
  
  <p>But when level 3 is no longer needed, the data structure will automatically delete[] level3.  I need to know that this will behave correctly and not just destroy everything in the array.  If it will then I need to just create new arrays and copy the contents over, but it would be nice to avoid doing that for performance reasons.</p>
  
  <p>Edit: Everyone seems to be jumping to the conclusion that I just should use a dynamic resizing container (ie vector, deque) in the first place for my data structure.  I am using levels of arrays for a good reason (and they aren't equally sized like I make it look like in my example).  I was merely looking for a good way to have a constructor to my data structure that takes in an array or vector and not need to copy the original contents over into the new data structure.  </p>