Perhaps you were expecting that new int[5]
would zero-initialise the new block of memory.
Well, it doesn't! And that's pretty much that. C++ doesn't do things you didn't ask for, because if you didn't want them, that's an unnecessary performance hit.
It looks like your new memory block just so happens to be found at the same place as the last one, and now you're [partially] observing the values left behind from the last one. Technically these values are unspecified, and reading them means your program has undefined behaviour. You have to provide your own fresh values for these new array elements before you can use them for anything truly meaningful.
Or perhaps you were expecting the delete[]
to zero-initialise the dying block of memory.
That doesn't happen either. There's really no point in zeroing-out objects just as they are about to wither out of existence. Again, C++ won't waste time on things like this.
You can make the zero-initialisation happen with new int[5]()
but I don't see a good reason to do so here.