I\'ve heard that you should usually \"delete\" whenever you use \"new\", yet when I run a simple test program (below), it doesn\'t seem to make a difference which numbers I put
No, it's not sufficient.
Each time you call new
or new[]
, some memory is allocated, and you are given the address of that memory (in order to be able to use it). Each piece of memory must eventually be delete
d (or delete[]
d).
You are storing that address in array
, but then immediately overwriting it on the next iteration. Therefore you have no way of delete
-ing all of pieces of memory that you've been allocated. Therefore you have a memory leak.