new and new again?

I just came up with something about new keyword.

here's code.

int *a = new int[10];
for(int i =0; i< 10; i++) a[i] =i;
a = new int[20];

then, I printed all, and the result is 0, 1, .., 9, -81823, -912938...
It looks working. So, I checked the address. something wrong.
the information of *a in memory is different from when I use int *a = new int[20];

Anyways, if i did like this, what happens??

I am just curious. any can explain?

Major memory leak!

Edit: Not as major as I first thought as the second new isn't in the loop, but it is still a memory leak.
Last edited on
Ya, you're right.
I tested it, then second a(new int[20]) refer to different address from first a(new int [10]).
Also, i will post something related to it again :)
I hope you will help me again :)
Topic archived. No new replies allowed.