Behind the scenes there is memory allocated, a little chunk of silicium somewhere in your machine is now dedicated to the array you just new
ed.
When you want to "resize" your array, it is only possible to do so in place if the chunk of silicium has some free space around it. Most of the times, it is instead necessary to reserve another, bigger, chunk and copy the data that were in the first... and obviously relinquish the first (otherwise you have a memory leak).
This is done automatically by STL containers (like std::vector
or std::deque
), but manually when you yourself call new
. Therefore, the best solution to avoid leaks is to use the Standard Library instead of trying to emulate it yourself.